file compare and make a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file compare and make a new file
# 1  
Old 05-05-2009
file compare and make a new file

Hi,

I need to make a file3 from the comparision of file1 and file2...

file1

x1,x2,x3,x4

file2

y1,y2,y3,y4

if (x2=y3) then file3

x1,x3,y1,y3,y4


Thanks
# 2  
Old 05-05-2009
what is the logic ,plz specify clearly..
I am not getting it ...!!
# 3  
Old 05-05-2009
Also, do these files 1 and 2 have the same record or not? Does it need to be compare line by line ( are they sorted or does it require soting)
# 4  
Old 05-05-2009
Thanks for your reply. I am sorry for not explaining my question throughly....

Both files are not same. both files will have different records. Only one column in both the files will be same. All the rows in both the files will be unique. Take an example:
File 1:
Department, Date, Place, City
File2:
X1, X2, Place, X3
I need to get only those rows from file 1 where 'Place' in file 2 equal to 'Place' in file1 and then make the file 3 with the columns of file1 and file2 as follows:
File3:
Department, Date, X1,X2,X3
# 5  
Old 05-05-2009
Is it possible? I am trying to explain more:

File1:
51999900,34.55,2/1/2009,"XYZ"
File2:
51999900,EUROPE AREA 1
now compare on column1 of file1 with column1 of file2...if matches then put the 2nd column value of file2 in the end of 1st file....
file3:
51999900,34.55,2/1/2009,"XYZ",EUROPE AREA 1
if not matches then write that row of file1 to a file4
# 6  
Old 05-05-2009
Not sure if you are looking for this

Code:
awk -F "," 'NR==FNR{a[$1]=$2;next}{
if ($1 in a) print $0,a[$1] > "file3"
else print $0 > "file4"
}' OFS="," file2 file1


cheers,
Devaraj Takhellambam
# 7  
Old 05-05-2009
Thanks for your suggestion. I get the following error......Please suggest...

syntax error The source line is 1.
The error context is
NR==FNR{a[$1]=$2;next}{if ($1 in a) print $0,a[$1] > "file3" >>> else <<< print $0 > "file4"}
awk: The statement cannot be correctly parsed.
The source line is 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

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

4. Shell Programming and Scripting

How to make multiple small file out of a single file?

Hi, I have a file that consist of around six million line, now the task is to divide this file into 12 small file so that each file would have half a million lines in it. Thanks. (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. Shell Programming and Scripting

make the name of file and fetch few things from log file

Hello All, I am working on a script where I need to fetch the value from a log file and log file creates with different name but few thing are common DEV_INFOMGT161_MULTI_PTC_BLD01.Stage_All_to_stp2perf1.042312114644.log STP_12_02_01_00_RC01.Stage_stp-domain_to_stp2perf2.042312041739.log ... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

7. Shell Programming and Scripting

how to make my own file as a running log file in bash

Hi, I have written a small script from that iam appending the output to a file.If multiple users invoke the same script or if i invoke the same script n number of times (using &), the output file(ZZ/OUT) contains messup information. #!/bin/bash # echo "Hello" >> /tmp/ZZ/OUT sleep 10 echo... (4 Replies)
Discussion started by: blrguest
4 Replies

8. Programming

makeutility: how to get the make-file name inside of the make-file?

How I can get the current make-file name in a make-file So, if I run make with specified file:make -f target.mak is it possible to have the 'target' inside of the that 'target.mak' from the file name? (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

Paste content of a file to another file and make it as columned

Pls help me on this. I have to 2 files like shown below: File 1 TAIJM AXPKIM BEMGW File 2 PXMPA JYGE IMJP What i want to do is to paste both file to a new file on thir format: File 3 TAIJM PXMPA AXPKIM JYGE BEMGW IMJP I tried cat and print, but it doesn't work. Cn... (6 Replies)
Discussion started by: kingpeejay
6 Replies

10. Shell Programming and Scripting

compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different. for example: Contents of file1 IP=192.165.89.11 NM=255.255.0.0 GW=192.165.89.1 Contents of file2 IP=192.165.89.11 NM=255.255.255.255 GW=192.165.89.1 NOTE HERE THAT NM IS DIFFERENT So i want the changes... (6 Replies)
Discussion started by: pradeepreddy
6 Replies
Login or Register to Ask a Question