$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
$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
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
$ 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.