Grepping or awking multiple lines in a file - regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping or awking multiple lines in a file - regex
# 1  
Old 08-11-2016
Grepping or awking multiple lines in a file - regex

data.txt:

Code:
hellohellohello
mellomello1mello
tellotellotellotello
bellobellowbellow
vellow

My attempts:

Code:
egrep ".*mello1\n.*bellow" data.txt
awk '/.*mello1.*\nbellow/' data.txt


how can i search for patterns that are on different lines using simple egrep or awk?

i only want the egrep or awk command to output data IF and only IF the output of data.txt contains mello1 and bellow (on different lines).
# 2  
Old 08-11-2016
Code:
sed -n '/mello1/,/bellow/p' data.txt

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-11-2016
Quote:
Originally Posted by rdrtx1
Code:
sed -n '/mello1/,/bellow/p' data.txt


under normal circumstances, i'd be content with this.
however, the script being used here i have no control over modifying it.

i need to pass a regex that can be fed as an argument to the script.

which is why i was using the egrep and awk. i need to be able to pass a variation of this to the script:

Code:
".*mello1.*\n.*bellow.*"


Last edited by SkySmart; 08-11-2016 at 02:35 PM..
# 4  
Old 08-11-2016
Hello SkySmart,

I am not completely sure about your requirement but could you please try following and let us know how it goes then.
Code:
awk -vs1="mello.*ello"  '{match($0,s1);if(substr($0,RSTART,RLENGTH)){print}}  Input_file

Output will as follows.
Code:
mellomello1mello

In case you want to assign a shell variable's value to awk's variable then following may help you in same.
Code:
S="mello.*ello"    ##Shell variable
awk -vs1=$S '{match($0,s1);if(substr($0,RSTART,RLENGTH)){print}}'  Input_file

Output will be as follows.
Code:
mellomello1mello

Just wanted to add here above was tested in GNU awk.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 08-11-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

I am not completely sure about your requirement but could you please try following and let us know how it goes then.
Code:
awk -vs1="mello.*ello"  '{match($0,s1);if(substr($0,RSTART,RLENGTH)){print}}  Input_file

Output will as follows.
Code:
mellomello1mello

In case you want to assign a shell variable's value to awk's variable then following may help you in same.
Code:
S="mello.*ello"    ##Shell variable
awk -vs1=$S '{match($0,s1);if(substr($0,RSTART,RLENGTH)){print}}'  Input_file

Output will be as follows.
Code:
mellomello1mello

Just wanted to add here above was tested in GNU awk.

Thanks,
R. Singh

the expected output should be something like:

Code:
egrep ".*mello1\n.*bellow" data.txt

mellomello1mello
tellotellotellotello
bellobellowbellow

# 6  
Old 08-11-2016
You might want to make a file for your sed commands:
Code:
/mello1/{
N
.........
}

After you find mello1 the N will append the next line to the "pattern space" so you could check for "mello1.*bellow" in place of the dotted line.
This User Gave Thanks to wbport For This Post:
# 7  
Old 08-11-2016
If your input file contains just:
Code:
hellohellohello
mellomello1mello
tellotellotellotello

should there be any output?

If your input file contains:
Code:
hellohellohello
mellomello1mello
tellotellotellotello
bellobellowbellow
fellow
hellohellohello
mellomello1mello
tellotellotellotello
bellobellowbellow
vellow

what should the output be? (From the 1st mello1 to the last bellow? Or two distinct sets from mello1 to bellow?)

If the input file contains:
Code:
hellohellohello
mellomello1mello
tellotellotellotello
fellow
hellohellohello
mellomello1mello
tellotellotellotello
bellobellowbellow
vellow

what should the output be? (From the 1st mello1 to the bellow? Or from the last mello1 to the bellow?)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

3. Shell Programming and Scripting

Grepping multiple lines in a file

HI I have a file with output as System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 I need to grep the values of following column fields u, s, w and i from each row sum them up and store in a variable..:( Please help.. (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Programming

Grepping a column from multiple file

I have 20 files that look pretty much like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (5 Replies)
Discussion started by: kayak
5 Replies

6. Shell Programming and Scripting

regex matches from lines in file

Hello, I try to script something (bash-script) and can not find a way to get and store a match into a variable from a line in a file. grep isn't useful as the matches are not returned - just colored. I can't get 'expr' to work for me. Is it necessary to use a perl-script with regex instead? ... (7 Replies)
Discussion started by: daWonderer
7 Replies

7. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

8. Shell Programming and Scripting

grepping all lines of one file from another file

Hi First post on here hope you can help with something I have a file with a couple of thousand lines (all lines are one string long, i.e a number I have another file that is over 1 million lines long Example entry from file 1 123456 Example from file 2 123456 mjhyuihbn ... (5 Replies)
Discussion started by: nampahc
5 Replies

9. Shell Programming and Scripting

awking and grepping parts of files: the 'super diff'

OKAY---- Here's what I must do. I have two files. I need to compare the two files such as with the diff command. I am adding FILENEW to FILEOLD If fields $1, $2, $5, and 6 are the same, then I don't want to add FILENEW records to FILEOLD. If they are not, then append the lines. Is... (11 Replies)
Discussion started by: jeffpas
11 Replies

10. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies
Login or Register to Ask a Question