The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help Required: Command to find IP address and command executed of a user loggedout Security 2 08-06-2008 05:12 PM
how to? launch command with string of command line options TinCanFury Shell Programming and Scripting 5 04-28-2008 03:06 PM
inconsistent ls command display at the command prompt & running as a cron job rajranibl Linux 5 07-30-2007 05:26 AM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr HP-UX 1 10-16-2006 01:16 PM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr Shell Programming and Scripting 0 09-19-2006 06:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-14-2007
Registered User
 

Join Date: Sep 2005
Posts: 15
awk command

I am making an awk command file to run on my UNIX box. I have 2 files:

file 1 file 2

1111 Jones/Ron ,P,2222
2222 Smith/John ,P,7788
3333 Smith/Jim ,P,9999
4444 Carter/Jane ,P,4444

I want to compare these 2 files. If there is anybody in file 2 that has the 3rd field match up with the field in file 1, I want to make a new file with their information.
In this example, I would have a new file (file 3) that would contain:

Jones/Ron ,P,2222
Carter/Jane ,P,4444

I want to be able to do this in the command file format:
example awk -f (name of my awk command file) File 1 > File 3

How do I do this?

Thanks,
Nick
Reply With Quote
Forum Sponsor
  #2  
Old 08-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk -F"," 'BEGIN{ while(getline < "1" ) { arr[$0]=i++ } } { split($0, now, ","); if ( now[3] in arr ) { print } }' 2
Reply With Quote
  #3  
Old 08-14-2007
Registered User
 

Join Date: Sep 2005
Posts: 15
awk

And how would I run this in the command file format?

thanks
Reply With Quote
  #4  
Old 08-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by nickg View Post
And how would I run this in the command file format?

thanks
Sorry your question is not clear.

Are you looking for something like,

to use the command in a script.awk file... something like that..

then just include the command posted into a script.awk file.
Reply With Quote
  #5  
Old 08-14-2007
Registered User
 

Join Date: Sep 2005
Posts: 15
awk

Please forgive me for asking so many questions. I am just learning awk and I have a longgggg way to go.

Ok, so I put your script into an awk file called AWK1. I want to run it like this:

awk -f AWK1 file 2 > file 3

I want the output to go to file 3. I need file 2 to reference file 1. This would be done in your awk script. I don't see the file references in your awk script.

I put examples of file 1 and file 2 in my original post.
Reply With Quote
  #6  
Old 08-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
file 1 ==> 1
file 2 ==> 2

redirect the output of the awk command to file3

awk command > file 3
Reply With Quote
  #7  
Old 08-14-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,030
nawk -F',' -f nickg.awk file1 file2

nickg.awk:
Code:
FNR==NR {arr[$1]; next }
$3 in arr
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0