With sed
you can use a range and q
uit input at a single completion:
sed '/^182$/p;//,/^ABC$/!d;/^ABC$/!d;q'
Similarly w/ GNU grep
you can split the input between two grep
s:
{ grep -nxF -m1 182; grep -nxF -m1 ABC; } <<\IN
123
XXY
214
ABC
182
558
ABC
856
ABC
IN
... which prints...
5:182
2:ABC
... to signify that the first grep
found a -F
ixed-string literal, -x
entire-line 182 match 5 lines from the start of its read, and the second found a similarly typed ABC match 2 lines from the start of its read - or 2 lines after the first grep
quit reading at line 5.
From man grep
:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching
lines. If the input is standard input from
a regular file, and NUM matching lines are
output, grep ensures that the standard input
is positioned to just after the last
matching line before exiting, regardless of
the presence of trailing context lines.
This enables a calling process to resume a
search.
I used a here-document for the sake of reproducible demonstration, but you should probably do:
{ grep ...; grep ...; } </path/to/log.file
It will also work with other shell compound-command constructs like:
for p in 182 ABC; do grep -nxFm1 "$p"; done </path/to/log.file
grep
is used? I don't think this can be done withgrep
but it would be easy withawk
orsed
(alone or in combination withgrep
). – Hauke Laging Jan 10 '15 at 14:45grep
is not required. I am not so familiar yet withsed
orawk
. If you have a good solution, let me hear it! :) @don_crissti only the first line should be printed. I don't care about the other occurrences. – Kolja Jan 10 '15 at 15:23