AWK file comparing and composing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK file comparing and composing
# 1  
Old 05-12-2011
AWK file comparing and composing

Hi all,

I'm less than newbie with AWK, but I'd like to learn more with your help!

This is my problem.

I've two files.

File A
Code:
test_a component_b 
test_b component_k
test_c component_d
test_g component_b
test_k component_a
....

The first column is a key (there is ONE test_a in the file....)

File B
Code:
test_a note1 
test_b note5 
test_c note1 
test_p note a

The first column is again a key

I should get
Code:
test_a component_b note 1
test_b component_k note 5
test_c component_d note 1
test_g component_b default
test_k component_a default


What should I get is a file that have:

- for each key from FILE B that has been found in FILE A

KEY , component (taken from file A) note (taken from file B)

and then append all the keys in A (with the corresponding component) and "default note" that are not present in B

All the keys from A that has not found in B should be discarded.

Thanks in advance!

Last edited by zaxxon; 05-12-2011 at 01:20 PM.. Reason: code tags
# 2  
Old 05-12-2011
Try the join command.
Code:
$
$ cat fileA
test_a component_b
test_b component_k
test_c component_d
test_g component_b
test_k component_a
$
$
$ cat fileB
test_a note1
test_b note5
test_c note1
test_p note a
$
$
$ join fileA fileB ; join fileA fileB -v 1 | sed 's/$/ default/'
test_a component_b  note1
test_b component_k note5
test_c component_d note1
test_g component_b default
test_k component_a default
$

# 3  
Old 05-12-2011
Code:
awk 'NR==FNR{a[$1]=$2;next}{print $0, (a[$1])?a[$1]:"default"}' FileB FileA

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 05-13-2011
Thanks rdcwayx,

I've reached the same (I HOPE!) result with a different code. Can you help me to understand the meaning of NEXT command?

And, Thanks again for your support!
# 5  
Old 05-13-2011
"next" coommand tells awk to skip the remaining steps and continue with the next line.

regards,
Ahamed
# 6  
Old 05-16-2011
Hi,

thanks all for your help, below my code

Code:
BEGIN { 
     FS = ",";
     OFS = ",";
     print ("first","second", "third", "fourth");
     }
     FNR==NR { 
         a[$1]=$1; 
         b[$1]=$2;
         c[$1]=$3;
         next };
{
    if ($1 in a) {print (a[$1], $2, b[$1], c[$1])}
       
 else {print ($1, $2, "default", "0")}
       }


I know, it's a bit verbose ....but I'm learning.

Thanks you for your suggestion.

Last edited by Franklin52; 05-30-2011 at 07:32 AM.. Reason: Please use code tags
# 7  
Old 05-30-2011
That's fine, good try.

Some suggestions from your code.
1. save some typing.
Code:
print ("first","second", "third", "fourth");

===> print "first second third fourth";

2. No need

Code:
a[$1]=$1

So change to:

Code:
if ($1 in a) {print $1, $2, b[$1], c[$1]}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

2. Shell Programming and Scripting

Awk:Comparing and matching two file

Dear All, I have 2 files as follows file1.txt 261187210101|r 261187210101|r 261187220101|y 261187220102|y 261187220103|y 261187231011|b 261187231011|b 261187241012|g 261187241012|g file2.txt 1187220 1187241 output: file1 |file2 |match (3 Replies)
Discussion started by: sayandri
3 Replies

3. Shell Programming and Scripting

Comparing two csv file fields using awk script

Hi All, I want to remove the rows from File1.csv by comparing the columns/fields in the File2.csv. I only need the records whose first column is same and the second column is different for the same record in both files.Here is an example on what I need. File1.csv: RAJAK|ACTIVE|1... (2 Replies)
Discussion started by: rajak.net
2 Replies

4. UNIX for Advanced & Expert Users

Comparing two files using awk

i have one file say file1 having many records.Each record contains 2000 characters.i have to compare 192-200 (stored as name)characters in this file from other file say file2 having name stored in 1-9 characters. after comparing i have to print the record from file1 in another file say file3 ... (3 Replies)
Discussion started by: sonam273
3 Replies

5. Shell Programming and Scripting

Comparing 2 files with awk and updating 2nd file

file1: (unique files) 1 /pub/atomicbk/catalog/catalog.gif 693 2 /pub/atomicbk/catalog/home.gif 813 3 /pub/atomicbk/catalog/logo2.gif 12871 4 /pub/atomicbk/catalog/sleazbk.html 18338 file2: (duplicate filenames allowed) 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813... (2 Replies)
Discussion started by: jontjioe
2 Replies

6. Shell Programming and Scripting

comparing awk and nawk

Hi Guys, i tried these two commands. First in awk and nawk. The nawk command is running fine but the awk command is throwing error. What is wrong with the awk command. There are lot of awk commands running fine in my system d003:/usr/local/dsadm/dsprod>nawk 'NR = 1 {print " "$0}' a.txt ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

awk - comparing files

I've been trying to use awk to compare two files that have pretty much the same data in apart from certain lines where in one file a fields value has changed. I want to print the line from the first file and the changed line from the second file. At the moment, all I can get it to do is print the... (6 Replies)
Discussion started by: dbrundrett
6 Replies

8. Shell Programming and Scripting

Comparing two files using awk

Hi, I have 2 text files n1 and n2. cat n1 : -------- 1234567890 4444444444 cat n2 : --------- 1234567890 4444444444 7777777777 8888888888 they are some sample records. File n1 contains some lookup records, whereas file n2 contains some transaction records. I need to get the... (2 Replies)
Discussion started by: kanu_kanu
2 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

10. Shell Programming and Scripting

comparing two files using awk.

Hi All, a new bie to awk, How to compare substring of col1,file 1 with col2file2 and get file1contents+col3file2 as output. file1 ----- kumarfghh,23,12000,5000 rajakumar,24,14000,2500 rajeshchauhan,25,16000,2600 manoj,26,17000,2300 file 2 -------- 123,kumar,US, 123,sukumar,UK... (4 Replies)
Discussion started by: jerome Sukumar
4 Replies
Login or Register to Ask a Question