awk compare specific columns from 2 files, print new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk compare specific columns from 2 files, print new file
# 1  
Old 11-14-2011
awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3.

FILE1

Code:
1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04
1467 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04
1516 58.18800 6.73900 -3.85E+03 1.38E-01 6.57E+03 4.76E+04
1517 60.60200 3.37300 -3.98E+03 1.22E-01 6.57E+03 5.11E+04
1531 63.00000 0.00000 -4.21E+03 8.37E-02 6.57E+03 6.41E+04
1548 53.72552 11.23098 -3.47E+03 5.58E-02 1.05E+04 8.66E+04

FILE2
Code:
1464   49.80800   30.74800  3.05E+02  5.44E-02  4.24E+04  1.66E+05 -2.89E-01  2.03E-08
1465   52.46520   19.78234  1.52E+02  5.43E-02  3.51E+04  1.42E+05 -1.16E+01  2.92E-08
1466   55.27433   14.72050 -2.52E+03  3.00E-01  1.05E+04  2.57E+04  4.80E+01  3.41E-06
1467   55.27433   14.72050 -2.52E+03  3.00E-01  1.05E+04  2.57E+04  4.80E+01  3.41E-06
1468   60.77000    6.24100 -3.25E+03  3.00E-01  6.57E+03  1.82E+04  4.94E+01  3.72E-06

FILE3
Code:
1464   49.80800   30.74800  3.05E+02  5.44E-02  4.24E+04  1.66E+05 -2.89E-01  2.03E-08
1465   52.46520   19.78234  1.52E+02  5.43E-02  3.51E+04  1.42E+05 -1.16E+01  2.92E-08
1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04
1467 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04
1468   60.77000    6.24100 -3.25E+03  3.00E-01  6.57E+03  1.82E+04  4.94E+01  3.72E-06

---------- Post updated at 11:11 PM ---------- Previous update was at 10:46 PM ----------

I was able to do this by first extracting the lines from FILE2 that are in FILE1 > tmp using the post linked to above. Then I used to diff to get the remaining lines from FILE2 > tmp2. I edited tmp2 to remove extraneous symbols and used cat to combine tmp2 and FILE1. sort -n was the final command needed.
# 2  
Old 11-14-2011
Pretty standard problem:
Code:
awk 'FNR==NR { r[$1] = $0; next; } r[$1] { print r[$1]; next } { print }' FILE1 FILE2 >FILE3

where FILE2 contains the ORIGINAL lines, and FILE1 contains the ones you want to replace, keyed on column 1.

Last edited by otheus; 11-14-2011 at 10:48 AM.. Reason: correction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

2. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

3. Shell Programming and Scripting

Compare columns 2 files and print

File 1 has 16 columns so does File 2 I want to remove all records from File 2 that column 1 and column 16 match between file 1 and file 2 delimter of files is ~ (10 Replies)
Discussion started by: sigh2010
10 Replies

4. Shell Programming and Scripting

awk print specific columns one row at a time

Hello, I have the following piece of code: roleName =`cat $inputFile | awk -F';' '{ print $1 }'` roleDescription =`cat $inputFile | awk -F';' '{ print $2 }'` roleAuthProfile =`cat $inputFile | awk -F';' '{ print $3 }'` mappedUserID (5 Replies)
Discussion started by: pr0tocoldan
5 Replies

5. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

6. Shell Programming and Scripting

Compare specific columns between two files having different layouts

Hi, My requirement is that I need to compare two files. For example : The first file will be having 15 columns and the second file will be having just 10 columns. Example : File1 : abcd,abrd,fun,D000,$15,$236,$217,$200,$200,$200... (3 Replies)
Discussion started by: gpsridhar
3 Replies

7. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

8. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

9. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

10. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies
Login or Register to Ask a Question