LinuxQuestions.org
Review your favorite Linux distribution.
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 06-13-2012, 03:12 PM   #1
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Rep: Reputation: Disabled
Combine awk and grep?


I have two files:

$ cat inds
2
5
3

$ cat family
1 2 3 4
1 5 3 4
1 3 0 0
1 4 0 0
1 7 0 0
1 6 2 7
1 8 2 7

I want to write a new file with all the records in family that are NOT in inds, but matching only in field 2 of family. I foolishly tried

grep -v -f inds family

But of course it matches anywhere in the line, and it removes the last two lines as well because they have the 2 in field 3.

awk '{if($2!="2" && $2!="3" && $2!="5") print $0}' family

would work if my real inds file was not hundreds and hundreds of lines long... So basically, I am looking for something that can inverse-match every pattern from a long long list of patterns, but only for field 2.

I hope this makes sense. Thank you.
 
Old 06-13-2012, 03:38 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Among many ways to accomplish that, here's a quickie that will work in awk:
Code:
awk 'ARGIND == 1 { inds[$1] = 1; next } { if( ! ($2 in inds) )  print $0 }' inds family
It reads the entire inds file into an associative array using the first field as the index and setting an arbitrary value, then for each line in the second file (all subsequent files, actually) it checks to see whether field two exists as an index in the inds array.

Last edited by rknichols; 06-13-2012 at 03:40 PM.
 
1 members found this post helpful.
Old 06-13-2012, 03:59 PM   #3
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you, this is exactly what I was looking for!

I think I even understand how it does it.
 
Old 06-14-2012, 08:36 AM   #4
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
I am assuming rk is presenting a full layman version in case you are new, but here is the same code with the superfluous parts removed:
Code:
awk 'FNR==NR{inds[$1];next}!($2 in inds)' inds family
 
1 members found this post helpful.
Old 06-14-2012, 12:01 PM   #5
AHJ
LQ Newbie
 
Registered: Jun 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
The assumption was correct, I am new. Thank you for the short version, with some googling I think I understand what is going on.
 
  


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
Can I use grep inside awk? Helene Programming 10 09-29-2015 08:48 PM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 07:29 PM
Grep and AWK keogk Linux - General 10 01-04-2010 08:03 PM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

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

All times are GMT -5. The time now is 11:56 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