The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 02:16 PM
How to pass a parameter from one Shell-script to another Shell-script subodhbansal Shell Programming and Scripting 2 09-22-2007 02:19 AM
How to Run a shell script from Perl script in Parent shell? hifake Shell Programming and Scripting 16 08-28-2007 05:42 PM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 1 04-30-2007 05:43 AM
Have a shell script call another shell script and exit heprox Shell Programming and Scripting 2 11-20-2006 04:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Jun 2008
Posts: 14
Shell Script using awk

File1



9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY

9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT

9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD

9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY





File2

DPT.1072 CP~ Apr 1514283.914

DPT.1172 CP~ Apr 967882.506

DPT.1672 CP~ Apr 545199.000

DPT.1772 CP~ Apr 912656.817







Compare the column 11 of file 1 with the 1st column of file2. The 1st column of file2 contains a prefix dpt. also. We need to compare file2 with file1 and in the output we should get all the values from file2 that are present in file1. We should not remove dpt. from the output. It will always be 8 characters in 1st column of file2 (e.g DPT.1072).



The output should be:



DPT.1072 CP~ Apr 1514283.914

DPT.1172 CP~ Apr 967882.506

DPT.1672 CP~ Apr 545199.000



I am thankful for your help.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Apr 2008
Location: Jakarta
Posts: 9
perhaps like this:

awk -F "|" '{print $11}' testing|xargs -i grep {} testing1|awk '{print$1}' testing1

testing is inputfile1 testing1 is comparation file

thanks
Reply With Quote
  #3 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Apr 2008
Location: Jakarta
Posts: 9
if you want all the output from file2
awk -F "|" '{print $11}' testing|xargs -i grep {} testing1|awk '{print$0}' testing1
Reply With Quote
  #4 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Jun 2008
Posts: 14
Thanks for your reply. I will try this. Only thing I am not sure is the prefix DPT. in file2. We want to compare two files after removing prefix DPT. but we do NOT want to remove DPT. from the output. Means in the comparsion command only we need to compare without DPT.---------May be at the time of comparsion something like substr(col1 of file2, 4,8)...........

Thanks Much.
Reply With Quote
  #5 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Code:
awk -F"|" 'FNR==NR {a[$11]++; next} a[substr($0, 5, 4)] {print}' file1 file2
awk -F"|" field separator

FNR==NR {a[$11]++; next} buid an array with 11th field of file1 and give value 1 (default value 0+1) to it.

a[substr($0, 5, 4)] {print} if a[nnnn] is TRUE (1)---> print line from file2
Reply With Quote
  #6 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Posts: 14
Thank-You. It worked fine.
Reply With Quote
  #7 (permalink)  
Old 06-16-2008
Registered User
 

Join Date: Jun 2008
Posts: 33
hi can you say me what xargs is all about...


thanks
krips.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:32 PM.


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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0