delete everything before pattern including pattern using awk or sed

你说的曾经没有我的故事 提交于 2019-12-10 14:20:39

问题


aaa aaaa aaaa aaaa
sss ssss ssss ssss
ddd dddd dddd dddd
fff ffff ffff ffff
abc pattern asd fde 
111 222 333 444 555
666 777 888 999 000

Desired output : If the

111 222 333 444 555
666 777 888 999 000

回答1:


Just set a flag whenever the pattern is found. From that moment on, print the lines:

$ awk 'p; /pattern/ {p=1}' file
111 222 333 444 555
666 777 888 999 000

Or also

awk '/pattern/ {p=1;next}p' file

It looks for pattern in each line. When it is found, the variable p is set to 1. The tricky point is that lines are just printed when p>0, so that the following lines will be printed.

This is a specific case of How to select lines between two patterns? when there is no such second pattern.




回答2:


sed '1,/pattern/d' file

works for your example.

sed '0,/pattern/d' file

is more reliable.




回答3:


Another one sed solution:

sed ':loop;/pattern/{d};N;b loop' file.txt


来源:https://stackoverflow.com/questions/19047312/delete-everything-before-pattern-including-pattern-using-awk-or-sed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!