awk and &&


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk and &&
# 1  
Old 11-10-2014
awk and &&

Hello Everybody.

I am having trouble matching multiple strings within an instanza. I ONLY want it to print if it finds both strings within the range. Example input (newlines between groups inserted only for readibility):

Keywords to match are "artist" and "play count"
Code:
startgroup1
artist: beethoven
album: greatest hits
play count: 30
endgroup

startgroup2
artist: bach
album: the essentials
play count: 23
endgroup

startgroup3
artist: george
album: the lost recordings
endgroup

And the desired output is:
Code:
artist: beethoven
play count: 30
artist: bach
play count: 23

Notice it skipped the 3rd group because it did not have both matches. I have little experience with AWK but I tried to mash solutions together like
Code:
awk '/startgroup*/,/endgroup/ {if(/artist*|play*/) {print}}'

^ which works but it includes group 3. I could not figure out how to insert the && operator instead of the "|", which I assume is what needs to happen.
# 2  
Old 11-10-2014
Hello sudo,

Following may help in same.

Code:
awk '/startgroup/ {A=1} {if(A){if($0 ~ /artist/){S=$0;} if($0 ~ /play count/){print S ORS $0}}} /endgroup/ {A=0}'  Input_file

Output will be as follows.
Code:
artist: beethoven
play count: 30
artist: bach
play count: 23

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-10-2014
How about:
Code:
PLAY=30
ARTIST=beethoven
grep -A3 $ARTIST input_file | grep -B3 $PLAY

EDIT:
Read again your post and figured you wanted the occourence of play, and not the played 30...
In that case the line would be something like:
Code:
grep -B3 play input_file | grep -ve group -ve album
artist: beethoven
play count: 30
--
artist: bach
play count: 23


Last edited by sea; 11-10-2014 at 08:03 PM..
This User Gave Thanks to sea For This Post:
# 4  
Old 11-10-2014
sudo,
You could also try something like:
Code:
awk '
/^artist:/ { a = $0 }
/^play count:/ { p = $0 }
/^endgroup/ {
        if(a != "" && p != "") {
                print a
                print p
        }
        a = p = ""
}' input

sea,
Note that:
Code:
grep -B3 play input_file | grep -ve group -ve album

won't give the desired output with input like:
Code:
startgroup4
album: Best Hits of the 70's
play count: 32
endgroup

Note also that the 2nd -v in your grep command (shown in red above) doesn't hurt anything, but is redundant.
These 3 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-11-2014
Code:
awk '$2 ~ /artist/ && $4 ~ /play count/ {print $2 FS $4}' FS="\n" RS=  inputfile

This User Gave Thanks to Aia For This Post:
# 6  
Old 11-11-2014
Quote:
Originally Posted by Aia
Code:
awk '$2 ~ /artist/ && $4 ~ /play count/ {print $2 FS $4}' FS="\n" RS=  inputfile

Note that in the 1st post in this thread, sudo said "I am having trouble matching multiple strings within an instanza. I ONLY want it to print if it finds both strings within the range. Example input (newlines between groups inserted only for readibility)". So, using RS= to separate records will work with the sample input, but not with the OP's real input.

I didn't get the impression that the artist and play count records are guaranteed to be in any particular order or position either.
This User Gave Thanks to Don Cragun 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

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

4. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

5. Shell Programming and Scripting

replace & with & xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

6. Shell Programming and Scripting

awk and &&

Hi I need to make some checks inside an awk statement, but cannot come up with the the syntax for several checks under one awk statement, for example: grep -w ${bits} mydata | awk '{if ($3==1)&&($4==3)&&($5==1) print }' awk: syntax error near line 1 awk: illegal statement near line 1 In... (3 Replies)
Discussion started by: aoussenko
3 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

9. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

10. Shell Programming and Scripting

Awk: Version && nextfile

How can I find which version of Awk is installed? OpSystem is HPUX 11.x I am getting an error when trying to use the keyword nextfile and I dont know why! (Well, I can only assume that I have am using a version of Awk that does not support nextfile. However, according to O'Reilly, nextfile is... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question