With sed you can use a range and quit input at a single completion:
sed '/^182$/p;//,/^ABC$/!d;/^ABC$/!d;q'
Similarly w/ GNU grep you can split the input between two greps:
{ 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 -Fixed-string literal, -xentire-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
grepis used? I don't think this can be done withgrepbut it would be easy withawkorsed(alone or in combination withgrep). – Hauke Laging Jan 10 '15 at 14:45grepis not required. I am not so familiar yet withsedorawk. 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