Reputation: 987
I am trying to find the line containing string 7KFF_1 in a file. I want to print the line where it's located and the line right after. I tried this but I am not sure where I went wrong
awk '/7KFF_1/ { header = $0; next } { print header"\n"$0 }' sample.txt
NOTICE! I DON'T want to print the n lines after a match! I would like the current line where the match was made and the line right after that.
Upvotes: 2
Views: 907
Reputation: 11237
grep
or sed
if applicable?
grep -A1 '7KFF_1' file
will print the line and 1 Line after as too would
sed -n '/7KFF_1/{N;p}' file
Upvotes: 3
Reputation: 212484
Use a flag. If the flag is set, print the line (to print the line after the match). Then assign the flag depending on if the current line matches. Then print if the flag is set (to print the line that matches.)
awk 'f; {f = /7KFF_1/}; f' sample.txt
You can simplify this to:
awk 'f; f = /7KFF_1/' sample.txt
but I thought that was a bit too obscure for the initial presentation.
Upvotes: 1
Reputation: 1570
Try this:
sed -n '/pattern/,$p' YOUR_FILE | head -2
The first part prints all lines after the pattern, then pipe it to head -2
printing the line containing your pattern AND the following line.
Upvotes: 2