LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2011, 03:49 PM   #1
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Rep: Reputation: Disabled
matching substrings to multiple part of a line


Hi guys,

I want to find to print out lines of a chart only if a substring matched in multiple parts of that line. So for ex:

col1 col2 col3 col4
this-dog 0-dog that-dog that-dog
this 4 that-dog that-dog

In this example I would only print Row 2 because the substring dog is present in all instances. I have tried something like this, but it has not worked:

Code:
 awk '{if ($1 == /.dog/ && $2 == /.dog/ && $3 == /.dog/ && $4 == /.dog/){print}} file > output
Why won't this work (I don't get an error, I just get a file size of 0) and how can I fix it?

Thanks!
 
Old 10-28-2011, 04:06 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Your syntax is not correct, have a look at this:
Code:
 awk '{ if ( $1 ~ "dog" && $2 ~ "dog" && $3 ~ "dog" && $4 ~ "dog") { print } }' file > output
Hope this helps

GnuAWK - Comparison Operators

Last edited by druuna; 10-28-2011 at 04:09 PM. Reason: Added link
 
1 members found this post helpful.
Old 10-28-2011, 04:08 PM   #3
trbennett48
LQ Newbie
 
Registered: Apr 2011
Location: Oregon
Posts: 25

Rep: Reputation: 3
Try this... the number of fields it processes is based
NF (number of fields) which is set automatically by awk:

Quote:
awk '{ \
nbr_misses = 0; \
for(idx=1;idx<NF;idx++) \
{ \
loc = index($idx, "dog"); \
if ( loc == 0 ){ nbr_misses++; }
} \
if ( nbr_misses == 0 ) \
{ \
print; \
} \
}'
 
Old 10-29-2011, 05:43 PM   #4
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thank you for your help and suggestions - they worked!

If I wanted to take this further and only have awk print if a substring matches exactly 3 times in a line, how would I do this?

with the code that Druuna suggested (the simpler one) it will print output even if "dog" is only found in 1/3 lines (or 2/3 lines, but not exactly 3).
 
Old 10-30-2011, 01:49 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You don't seem to mention what if it is more than 3, but here is an alternative:
Code:
awk 'match($0,/(dog).*(dog).*(dog)/,f) && f[3]' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
Perl: Multiple line regex matching sammywammy Programming 4 02-09-2011 10:03 AM
multiple line pattern matching and variable extraction object95 Programming 1 10-19-2010 02:51 AM
perl matching multiple occurences on one line ShaqDiesel Programming 2 10-11-2009 07:13 AM
perl help for multiple line matching sal_paradise42 Programming 6 07-24-2008 07:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration