7
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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 2
    Try not to think and specify problems in terms of what you want to delete from a file but instead in terms of what you want to select from a file - it often makes a difference to the readability, maintainability, etc. of the solutions you (and we) come up with to your problem. – Ed Morton Sep 27 '13 at 10:00

3 Answers3

13

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    `next` could be saved... `awk 'p;/pattern/{p=1}'` ;) – Kent Sep 27 '13 at 09:41
  • I must say, your original answer is easier to read and understand. particularly if the script was long. – Kent Sep 27 '13 at 09:47
  • awk 'p; /pattern/ {p=1}' file –  Sep 27 '13 at 10:26
  • Beware though that when pattern isn't found at all, this deletes everything. – Martin von Wittich Jul 27 '15 at 09:42
  • @MartinvonWittich you are right. To prevent this, you can read the file twice: once to see if the pattern is found and set a flag. Then, to print it either completely or from the pattern: `awk 'FNR==NR {if (/pattern/) found=1; next} p || !found; /pattern/ {p=1}' file file` – fedorqui Jul 27 '15 at 09:53
8
sed '1,/pattern/d' file

works for your example.

sed '0,/pattern/d' file

is more reliable.

Kent
  • 189,393
  • 32
  • 233
  • 301
2

Another one sed solution:

sed ':loop;/pattern/{d};N;b loop' file.txt
sat
  • 14,589
  • 7
  • 46
  • 65