I have a text and I want to extract the 4 lines following a keyword!
For example if I have this text and the keyword is AAA
hello
helloo
AAA
one
two
three
four
helloooo
hellooo
I want the output to be
one
two
three
four
I have a text and I want to extract the 4 lines following a keyword!
For example if I have this text and the keyword is AAA
hello
helloo
AAA
one
two
three
four
helloooo
hellooo
I want the output to be
one
two
three
four
if your grep supports -A option, then use it like
grep -A 4 "AAA" file.txt
else, you can try with awk
awk '/AAA/{for(i=1;i<=4;i++){getline;print}exit;}' file.txt
with this code I get the line with the keyword too! I dont want this line!
Try:
awk 'p-->0; /AAA/{p=c}' c=4 infile
thank you! it works great!
if it's not trouble do you know how can I do this with grep?
AFAIK this cannot be done with grep alone...
ok! thnx again for the info
If the keyword appear only once:
sed -n '/AAA/{n;N;N;N;p;q}' infile
otherwise:
sed -n '/AAA/{n;N;N;N;p}' infile