Hello,
I want to search 2 lines from the file. I wanted to display all such matches. Example file:
Testfile is test
TEST1
TEST2
testing the file string
to do testing
TEST1
TEST2
sample strings
=================
I wanted to search the file with 2 lines
"
TEST1
TEST2
"
Output should display (If possible, line numbers also )-
TEST1
TEST2
TEST1
TEST2
============
Thank you
Yoda
2
Assuming strings: TEST1 & TEST2 are in adjacent lines:
awk '$0=="TEST1"{getline;if($0=="TEST2") { print NR-1, "TEST1"; print NR, "TEST2"; } } ' filename
RudiC
3
Isn't that overly complicated? Try
$ awk '/^TEST[12]$/ {print NR,$0}' file
3 TEST1
4 TEST2
7 TEST1
8 TEST2
BTW, the specification is not clear - do you want the lines of equals signs included or not? AND, pls use code tags as advised!
Oohhh - careful reading sometimes helps - you want the pattern to bspan two consecutive lines. Try this:
$ awk '/^TEST1$/{getline NL} NL~/^TEST2$/ {print NR-1, $0; print NR, NL} NL=""' file
or
$ awk '/^TEST1$/{OL=NR FS $0; getline; if ($0~/^TEST2$/) {print OL; print NR, $0}} ' file
sed -n '/TEST1/N; /\n.*TEST2/p' file
Line numbers:
nl -ba file | sed -n '/TEST1/N; /\n.*TEST2/p'