Comparing the two files using awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing the two files using awk script
# 1  
Old 02-10-2012
Comparing the two files using awk script

Hi all,
Can you please help me to find out that where is the problem in my script or either my way of writing the shell command on the prompt is not right? Actually, I want to compare the second column "$1" of the file "t1" with all the columns of second file "t2", if there is a match then the script should return the matched column. But currently my script is printing the whole data of the first file "t1". I will highly appreciate any feedback and suggestions. I badly need to fix this problem as soon as possible; therefore any quick response will be acknowledged.

File1 "t1":
Code:
   7 real_name
   8 pa_name
   9 make_server_info_pw
   9 passon
  11 mapped_name
  11 nt_status
  13 passon
  15 p
  17 server_info
  18 p

File2 "t2":
Code:
   1 CHECK_DECLS
   1 True
   1 conf
   1 headers
   1 reverse
   1 real_name

Script:
Code:
 #!/usr/bin/awk
  BEGIN {
  FS= “ ”;
  OFS=” ”; 
  RS= “\n”;
  ORS=”\n”;
  }
  NR==FNR
  {
  arr[$1]; next 
  }
  $1 in arr {
  print $1;  
  } file1 file2 

Shell command to execute the script
Code:
awk -f sim.awk t1 t2 

Desired out put
Code:
 real_name


Last edited by Franklin52; 02-11-2012 at 08:57 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-11-2012
Firstly, please use code tags for sample data and code. See video tutorial. Secondly, please post desired output for the sample input files.

---------- Post updated 02-11-12 at 02:40 PM ---------- Previous update was 02-10-12 at 03:21 PM ----------

Code:
awk 'NR==FNR{a[$2]=1;next}$2 in a{print $2}' t1 t2

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-11-2012
Thanks Franklin52 for editing my request and Bartus11 for the script.

Bartus11, please can you explain the following parts of the script?

Code:
what is the meaning of this ' before NR==FNR

Code:
Why you put the values of file1 in array "a" and why you have assigned the value 1 to column2  a[$2]=1

Code:
What is the meaning of this pattern $2 in a

Code:
Why you end the program with another single quote '

One again many thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk for comparing two files

so have file1 like this: joe 123 jane 456 and then file2 like this: 123 left right 456 up down joe ding dong jane flip flop what I need to do is compare col1 and col2 in file1 with col1 in file2 and generate a new file that has lines like this: joe 123 ding dong left right jane... (11 Replies)
Discussion started by: Jaymz
11 Replies

2. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

3. Shell Programming and Scripting

comparing 2 files with awk

Hi, I'm a new user in awk and i'm trying to compare two files to create a third one if some values match in both files. The first file has this content: s 45.960746365 _21_ AGT 2490 [21:0 22:0 s 45.980418496 _21_ AGT 2491 [21:0 22:0 s 46.000090627 _21_ AGT 2492 [21:0 22:0 s 47.906552206... (2 Replies)
Discussion started by: carlosoria
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 two files using awk

hit brick wall while trying to knock up a script that will take values from the "lookup" file and look it up in the "target" file and return values that dont appear in "target" but do in "lookup". just knocked up something using bits from previous threads but theres gotta be something wrong... (13 Replies)
Discussion started by: jack.bauer
13 Replies

6. Shell Programming and Scripting

awk script issue : comparing two files with a pattern

File 1 ################################################################# pma.zcal.iop_pma_zcal_cntl (2710.080 115.200) pma.lanea23.rx0.cntl (696.960 844.800) pma.lanea67.rx0.cntl (1733.760 844.800) pma.zcal.iop_pma_zcal_cust (2280.960 115.200)... (1 Reply)
Discussion started by: jaita
1 Replies

7. Shell Programming and Scripting

Awk script / comparing two files

Goal: To combine the data from two files into one file. File1 = 11 fields, the first field is the unique key File2 = 2 fields, the first field is the unique key What I want to do is match File2:column1 with File1:column1 and if it matches, to add the data from File2:column2 to the matching... (2 Replies)
Discussion started by: jmcgranahan
2 Replies

8. 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

9. 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

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