Difference between 2 files, one with 1 column and 2nd file with multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between 2 files, one with 1 column and 2nd file with multiple columns
# 1  
Old 04-22-2014
Difference between 2 files, one with 1 column and 2nd file with multiple columns

Hi,
I need to find the difference between 2 files in unix and write the result in the new file

File1:
Code:
A
B

File2:
Code:
X  123  hajkd
Y  345  adjfka
A  123   djafjhd
B  678   dsndjks

Output file:
Code:
X  123  hajkd
Y  345  adjfka


Thanks.

Last edited by vgersh99; 04-22-2014 at 06:14 PM.. Reason: code tags, please!
# 2  
Old 04-22-2014
Code:
awk 'FNR==NR {f1[$0];next} !($1 in f1)' file1 file2

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-22-2014
iam new to unix
in which file is it writing the results, can you please explain,
# 4  
Old 04-22-2014
Quote:
Originally Posted by nani1984
iam new to unix
in which file is it writing the results, can you please explain,
it should output to the screen. You can redirect that to a file of your choice:
Code:
awk 'FNR==NR {f1[$0];next} !($1 in f1)' file1 file2 > myOutputFile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 04-22-2014
vgersh99 THanks A Lot , Its worked.
# 6  
Old 04-24-2014
recevied
abc
def
ghi

totallist
abc 123 jasdhfaj
def 345 fjdgkfsfh
ghi 567 dfjdhdhfj
jkl 678 djkahfdjshdf
xyz 984 jdfdhfhdh


myOutputFile
jkl 678 djkahfdjshdf
xyz 984 jdfdhfhdh

I used this command for the output : awk 'FNR==NR {f1[$0];next} !($1 in f1)' recevied totallist > myOutputFile

Can any one explain the command, its hard to understand, why they used '$0' in first and '$1' in the second .

Last edited by nani1984; 04-24-2014 at 01:15 PM.. Reason: mistyping
# 7  
Old 04-25-2014
hey,

i dont know how to open a new thread , hence posting here, coz my question is related.
i want to compare 2 similar files. if the contents are same then i want to echo "no new data added OR files are same". if some new data is added, or there is difference in file, i want to print on difference , also save the difference in some third file.
[code]
file a:
name : xyz
age : ab
address : pqr, st
profession : engg

file b:
name : xyz
age : ab
address : pqr, st
profession : engg ETC // extra words added wrt file a
job : project engg // extra line added wrt file a

output file c :
profession : engg ETC // diff part is highlighted to differentiate to file a
job : project engg // extra line printed, since added line wrt file a

[\code]

Please enlighten.

Thanks and Regards,
Rishi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

3. Shell Programming and Scripting

Combining certain columns of multiple files into one file

Hello Unix gurus, I have a large number of files (say X) each containing two columns of data and the same number of rows. I would like to combine these files to create a unique merged file containing X columns corresponding to the second column of each file (with a bonus of having the first... (3 Replies)
Discussion started by: ksennin
3 Replies

4. Shell Programming and Scripting

Join 2nd column of multiple files

Dear All, I have many files formatted like this: file1.txt: 1/2-SBSRNA4 18 A1BG 3 A1BG-AS1 6 A1CF 0 A2LD1 1 A2M 1160 file2.txt 1/2-SBSRNA4 53 A1BG 1 A1BG-AS1 7 A1CF 0 A2LD1 3 A2M 2780 (5 Replies)
Discussion started by: paolo.kunder
5 Replies

5. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

6. UNIX for Dummies Questions & Answers

Extract columns from multiple files with a file name as heading

Hi All, I have 7 files, each containing 9 columns separated by space. I want to extract the 9th columns from every file and save in a new file. The columns must be pasted next to each other. And the title of each columns should be the name of the corresponding files! Since the 3rd column is... (1 Reply)
Discussion started by: Unilearn
1 Replies

7. Shell Programming and Scripting

Combining columns from multiple files to one file

I'm trying to combine colums from multiple file to a single file but having some issues, appreciate your help. The filenames are the same except for the extension, path1.m0 --------- a b c d e f g h i path1.m1 --------- m n o p q r s t u File names are path1.m The... (3 Replies)
Discussion started by: rkmca
3 Replies

8. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

9. Shell Programming and Scripting

Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data. I would like to generate one single file from all these files as follows: One instance of 1st column Text, followed by 2nd column Data separated by... (5 Replies)
Discussion started by: subhap
5 Replies

10. Shell Programming and Scripting

Merging columns from multiple files in one file

Hi, I want to select columns from multiple files and combine them in one file. The files are simulation-data-files with 23 columns each and about 50 rows. I now use: cut -f 11 Sweep?wing-30?scale=0.?0?fan2?.txt | pr -3 | awk '{printf("\n%s\t%s\t%s",$1,$2,$3)}' > ../Data_Processed/output.txtI... (1 Reply)
Discussion started by: isgoed
1 Replies
Login or Register to Ask a Question

Featured Tech Videos