Compare two files and write data to second file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and write data to second file using awk
# 1  
Old 04-23-2014
Tools Compare two files and write data to second file using awk

Hi Guys,

I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data.

Example:

Delimited File
--------------
Code:
Byer;Amy;NONE1;A5218257;E5218257
Byer;Amy;NONE1;A5218260;E5218260

Positional File
--------------
Code:
190004000153E5218257010000000980002425852013
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013
190004000153E5218261010000012190002425852013

If the fifth filed in the delimited file is found in the positional file at position 13 then we have to append the fourth field from the delimited file into the positional file, at the end of the respective record.

Expected output
===============
Code:
190004000153E5218257010000000980002425852013A5218257
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013A5218260
190004000153E5218261010000012190002425852013

Script Used
===========
Code:
awk 'BEGIN{FS = ";"};FNR==NR{A[$5] = $4}
FNR!=NR{if ((x = substr($0,13,8)) in A) print $0 A[x];else print A[x]}' $1 $2 > $3


The above awk command is not working properly. It is not appending the desired data in the positional file.

Please help.

Thanks in advance!!

Thanks,
Ajay

Last edited by Scrutinizer; 04-23-2014 at 09:44 AM.. Reason: Please use code tags; mod2: spelling
# 2  
Old 04-23-2014
Try this:
Code:
awk -F; 'NR==FNR{a[$5]=$4; next} {s=substr($0,13,8)} s in a {$0=$0 a[s]}1' file1 file2

# 3  
Old 04-23-2014
Tools

Thanks For the reply Frank, it is still not working It is writing the second file as it is in the out put file. It is not appending the data in the record.

Can l you please let me know why are you adding 1 at after the append function?
Code:
awk -F; 'NR==FNR{a[$5]=$4; next} {s=substr($0,13,8)} s in a {$0=$0 a[s]}1' file1 file2

Thanks,
Ajay

Last edited by Scrutinizer; 04-23-2014 at 02:21 PM.. Reason: code tags
# 4  
Old 04-23-2014
In awk, any nonzero numeric value or any nonempty string value is true.

So 1 means true and if true, the default awk operation is print record:
Code:
awk 1 filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

5. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

6. Shell Programming and Scripting

How to compare data from 2 zip files and capture the new records from file2 to a new file

I have 2 zip files which have about 20 million records in each file. file 2 will have additional records than file 1. I want to compare the records in both the files and capture the new records from file 2 into another file file3. Please help me with a command/script which provides me the desired... (8 Replies)
Discussion started by: koneru
8 Replies

7. Shell Programming and Scripting

Extract data with awk and write to several files

Hi! I have one file with data that looks like this: 1 data data data data 2 data data data data 3 data data data data . . . 1 data data data data 2 data data data data 3 data data data data . . . I would like to have awk to write each block to a separate file, like this: 1... (3 Replies)
Discussion started by: LinWin
3 Replies

8. Shell Programming and Scripting

Compare two csv files by two colums and create third file combining data from them.

I've got two large csv text table files with different number of columns each. I have to compare them based on first two columns and create resulting file that would in case of matched first two columns include all values from first one and all values (except first two colums) from second one. I... (5 Replies)
Discussion started by: agb2008
5 Replies

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question