LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2012, 01:53 PM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
awk power


I have 2 files as attached.

f1 contain a lots of lines like this:

Quote:
319030003003~319030084005~1~0070~EA~
399056084020~319030084006~1~0080~EA~
319056003001~399056084019~1~0010~EA~
319030084005~399056084020~1~0020~EA~
319030084006~319056084001~1~0030~EA~
319030003089~319030084005~1~0070~EA~
399056084029~319030084006~1~0080~EA~
f2 contains

Quote:
319030084006~
319056003001~
319030084005~
So using awk I want to delete the lines in f1, in which the 1st column contains the number in f2 and save in f3.

I want in f3:

Quote:
319030003003~319030084005~1~0070~EA~
399056084020~319030084006~1~0080~EA~
319030003089~319030084005~1~0070~EA~
399056084029~319030084006~1~0080~EA~
How can I do this using awk ??
Attached Files
File Type: txt f1.txt (25.5 KB, 10 views)
File Type: txt f2.txt (56 Bytes, 8 views)
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-02-2012, 02:08 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
awk -F~ 'FNR == NR { _[$1]++ } FNR < NR && !_[$1]' f2 f1 > f3
This uses the TRUE and FALSE interpretation of awk: reading f2 it increases the value of the array _ with index $1; if the number has been encountered in f2, the value of _[$1] is greater or equal to 1, that is TRUE, hence the negation is FALSE and the matching record in f1 is not printed out.

On the contrary, when it encounters a line in f1 whose first field was not listed in f2, the evaluation of _[$1] returns an empty string (that is FALSE) and the negation makes it TRUE: indeed we want these lines be printed out!

Hope this helps.

Last edited by colucix; 06-03-2012 at 11:12 AM. Reason: For the sake of completness!
 
3 members found this post helpful.
Old 06-02-2012, 02:15 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Just for fun, here's a quick&dirty solution using grep, sed, and bash's process substitution.

Code:
grep -v -f <( sed 's/^/^/' f2 ) f1
The sed p.sub is there to alter the contents of f2 first into regexes that only match the beginning of the line.
 
1 members found this post helpful.
Old 06-08-2012, 06:09 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
Although the question was for awk:
Code:
$ join -v 1 -t "~" <(sort f1) <(sort f2)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
running series of awk commands from an awk file oreka18 Programming 3 05-16-2012 01:13 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration