I have a file A of the form (frequency,file name,code lines):
1 file_name1 code_line1
2 file_name2 code_line2
2 file_name2 code_line3
2 file_nam
To add to the awk and Perl solutions, a GNU sed solution:
$ sed -r 'N;/file_name(\w+).*\n.*file_name\1/!{s/\n/&\n/;P;s/^[^\n]*\n//};P;D' infile
1 file_name1 code_line1
2 file_name2 code_line2
2 file_name2 code_line3
2 file_name3 code_line4
2 file_name3 code_line5
3 file_name4 code_line6
3 file_name4 code_line7
3 file_name4 code_line8
Explained:
N # Append next line to pattern space
# If the numbers after the 'file_name' string DON'T match, then
/file_name(\w+).*\n.*file_name\1/! {
s/\n/&\n/ # Insert extra newline
P # Print up to first newline
s/^[^\n]*\n// # Remove first line in pattern space
}
P # Print up to newline - if we added the extra newline, this prints the empty line
D # Delete up to newline, start new cycle