awk: Multiple search patterns & print in an one liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: Multiple search patterns & print in an one liner
# 1  
Old 06-30-2011
awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work

Code:
gawk -F[.:] '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}'

Can anybody help getting the right code?

Last edited by radoulov; 06-30-2011 at 05:13 PM.. Reason: Code tags.
# 2  
Old 06-30-2011
Print the line or the pattern?

This will print only lines containing pattern1 and pattern2
Code:
awk '/pattern1/ && /pattern2/'


Last edited by neutronscott; 06-30-2011 at 05:12 PM.. Reason: code tags
# 3  
Old 06-30-2011
Quote:
Originally Posted by neutronscott
Print the line or the pattern?

This will print only lines containing pattern1 and pattern2
Code:
awk '/pattern1/ && /pattern2/'

Tanks neutronscott.

Got this far too, but pattern1 is in a different line than pattern2 and i would like to print $1 of line $0 found with pattern1 and $2 of line $0 found with pattern2
# 4  
Old 06-30-2011
Code:
gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) print pat1, pat2}' file

# 5  
Old 06-30-2011
So it's just one match per file?
# 6  
Old 06-30-2011
Quote:
Originally Posted by shamrock
Code:
gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) print pat1, pat2}' file

Yes this works! Thanks! Though it prints some lines multiple the same time. Like this:
Code:
4827  235,-- EUR
4827  235,-- EUR
4830  235,-- EUR
4830  235,-- EUR
4830  235,-- EUR
4830  383,-- EUR

Depending on the number of lines in a file.

Last edited by Franklin52; 07-02-2011 at 05:41 AM.. Reason: Please use code tags, thank you
# 7  
Old 07-01-2011
Quote:
Originally Posted by sdf
Yes this works! Thanks! Though it prints some lines multiple the same time.
yeah i should have caught that mistake so here's the updated version...
Code:
gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) print pat1, pat2;pat=pat2=""}' file

This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print between multiple patterns

Hello Gurus, I have a file this Dir Path 1 Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields Dir Path2 Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource name"="DS name2";some other fields. Under each dir... (14 Replies)
Discussion started by: sirababu
14 Replies

2. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

3. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

4. Shell Programming and Scripting

awk Help: Filter Multiple Entry & print in one line.

AWK Gurus, data: srvhcm01 AZSCI srvhcm01 AZSDB srvhcm01 BZSDB srvhcm01 E2QDI31 srvhcm01 YPDCI srvhcm01 YPDDB srvhcm01 UV2FSCR srvhcm01 UV2FSBI srvhcm01 UV2FSXI srvhcm01 UV2FSUC srvhcm01 UV2FSEP srvhcm01 UV2FSRE srvhcm01 NASCI srvhcm01 NASDB srvhcm01 UV2FSSL srvhcm01 UV2FSDI (7 Replies)
Discussion started by: rveri
7 Replies

5. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

10. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies
Login or Register to Ask a Question