-2
 $cat data
 123
 ABC
 DEF
 GHI
 ABC
 EFG

Output should be

DEF
GHI
ABC
EFG

Matching word will be "ABC". So it will detect for the first occurrence of the word "ABC" and will print from next n lines after matching line

Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
  • See b or e at https://stackoverflow.com/a/17914105/1745001. Your requirements aren't clear so idk which one you need but it is one of those two best I can tell. – Ed Morton Sep 04 '17 at 13:10

2 Answers2

1

Short awk solution:

awk '/ABC/ && !f{ f=1;next }f' file
  • /ABC/ && !f - on encountering line with ABC and with unset mark/flag !f - set the flag f=1 as indicator of the 1st occurrence of ABC line; next - skip the current pattern line

  • f - print the line while the mark/flag is set

The output:

DEF
GHI
ABC
EFG
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0
$ sed '1,/ABC/d' data
DEF
GHI
ABC
EFG

This applies the delete function of sed (d) to all lines in the range 1 to /ABC/ (the first line that matches the pattern ABC). This deletes the first few lines, up to and including the line containing ABC.

The default action of printing all lines will be applied to the remaining lines.


Using awk:

$ awk '/ABC/ && !f && f = 1 { next } f' data
DEF
GHI
ABC
EFG

If ABC is found on a line and f is not set, set f to 1 and skip to the next line. If f is set, print the line, otherwise discard input.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • Your first example is not quite right, it only deletes `ABC` not all the preceding lines. I think you want `sed '1,/ABC/d' data` – Mark Setchell Sep 04 '17 at 07:46
  • @MarkSetchell You are absolutely correct. Sloppy typing. Fixing it now. It's what I wrote in the text... – Kusalananda Sep 04 '17 at 08:09
  • @MarkSetchell :-) In this case I had a wrong solution in the browser and a correct solution in my shell and decided, against my better judgement, to edit by hand rather than copy and paste... – Kusalananda Sep 04 '17 at 08:31