Sunday, June 5, 2022

[SOLVED] Find line that matches a substring, then print THAT line and the following

Issue

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.


Solution

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


Answered By - HatLess
Answer Checked By - Marilyn (WPSolving Volunteer)