How to Search string(which is in 2 lines) in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Search string(which is in 2 lines) in a file?
# 1  
Old 01-05-2013
How to Search string(which is in 2 lines) in a file?

Hello,

I want to search 2 lines from the file. I wanted to display all such matches. Example file:
==================
Code:
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
"
Code:
TEST1
TEST2

"

Output should display (If possible, line numbers also )-
=====
Code:
TEST1
TEST2
TEST1
TEST2

============

Thank you

Last edited by Scrutinizer; 01-05-2013 at 05:41 AM.. Reason: code tags
# 2  
Old 01-05-2013
Assuming strings: TEST1 & TEST2 are in adjacent lines:
Code:
awk '$0=="TEST1"{getline;if($0=="TEST2") { print NR-1, "TEST1"; print NR, "TEST2"; } } ' filename

# 3  
Old 01-05-2013
Isn't that overly complicated? Try
Code:
$ 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:
Code:
$ 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


Last edited by RudiC; 01-05-2013 at 05:49 AM.. Reason: Misread the request - correction
# 4  
Old 01-05-2013
Code:
sed -n '/TEST1/N; /\n.*TEST2/p' file

Line numbers:
Code:
nl -ba file | sed -n '/TEST1/N; /\n.*TEST2/p'


Last edited by Scrutinizer; 01-05-2013 at 06:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for string on a range of lines

Hi, I am trying to parse a file searching for specific set of string and then within those set of strings finding a keyword. The script works the way that I intended it to be but I thought it could be a lot simpler may be. Any advice will be much appreciated. The script at the moment is as... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Search for 2 string in 2 lines with sed ?

Hi ! I want to search a string in all lines with sed. If that string is there, i want to look for an other string in the next line.If that string is there i want to put an other line under it. eg: aaa bbb ccc ddd cat bla.txt | sed -e '/aaa/a\' -e ' \!!!' in the upper case, i would... (6 Replies)
Discussion started by: fugitivus
6 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

6. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

10. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies
Login or Register to Ask a Question