I think the best way is to use grep
in combination with cut
and tail
. First, use grep to get the line on which the desired string is (-n
to output line number; -m 1
to stop searching after the first match):
grep -n -m 1 "somestring" filename.txt
This outputs the line number and the string itself. To cut away the string, we use cut (-f1
: output first field; -d:
use ":" as delimiter):
grep -n -m 1 "somestring" filename.txt | cut -f1 -d:
Next, we use the output of this command as parameter in tail. Normally, tail prints the last k lines, but using -n +k
, we get tail to print from line k onwards. The total command is:
tail -n +`grep -n -m 1 "somestring" filename.txt | cut -f1 -d:` filename.txt
To output the lines until somestring
use head
instead of tail
and -n -#
instead of -n +#
. You could also combine both to get the lines from one string until another.
awk '/line 2A/,0'
and the second can beawk '/line 2A/,/A/&&!/line 2A/'
or if it's at least one char before the Aawk '/line 2A/,/[^2]A/'