Compare two txt files,mismatches will be in new txt files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two txt files,mismatches will be in new txt files
# 1  
Old 12-26-2013
Compare two txt files,mismatches will be in new txt files

Hi,
Below are the sample data for txt files.

txt file 1
Code:
Partnumber|catgroup_id
10001082|46016
10001093|4680
10001093|386003
10001093|463004
10003251|683
10003251|63005
10003252|463005
10003252|4683
10003260|463005
10003260|4683
10003264|4683
10003264|463005
13420000|67
23450986|1234


txtfile 2
Code:
Partnumber|catgroup_id
10001093|4680
10001093|234
10001093|40
10003251|4683
10003251|463005
10003252|46
10003252|4683
10003260|463005
10003260|468
13420000|67
23450986|123

For each partnumber present in txt file 2,we need to compare catgroup_ids for both the files corresponding to the partnumber,mismatches catgroupids and partnumber from txt file 2 will have to store in new txt file
*One part number might have multiple catgroupids.

Thanks for the help in advance.

Regards
Ankita

Last edited by bartus11; 12-26-2013 at 11:32 AM.. Reason: Please use code tags
# 2  
Old 12-26-2013
I guess I am lost with the long sentence there Smilie

Can you give me a sample output needed?

-----------------------------------

This could help Smilie

Code:
awk -F"|" 'FNR==NR{a[$1]++;next}!a[$1]' file1 file2


Last edited by PikK45; 12-26-2013 at 11:26 AM.. Reason: added awk
# 3  
Old 12-27-2013
Hi,

Thanks for the command.

Output file would be-
Code:
10001093|234
10001093|40
10003251|4683
10003251|463005
10003252|46
10003260|468
23450986|123

Ankita

Last edited by Scott; 12-27-2013 at 10:27 AM.. Reason: Please use code tags
# 4  
Old 12-27-2013
Code:
$ awk -F"|" 'FNR==NR{A[$1 FS $2];next}!(($1 FS $2) in A)' file1 file2 

Code:
$ grep -v -f <(sort file1) <(sort file2)

Code:
10001093|234
10001093|40
10003251|4683
10003251|463005
10003252|46
10003260|468
23450986|123

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 12-27-2013
@Akshay: guess my awk wouldn't give the desired o/p Smilie
# 6  
Old 12-27-2013
Quote:
Originally Posted by PikK45
This could help Smilie
Code:
awk -F"|" 'FNR==NR{a[$1]++;next}!a[$1]' file1 file2

Quote:
Originally Posted by PikK45
@Akshay: guess my awk wouldn't give the desired o/p Smilie
following would give output but }!a[$1,$2]' is not good, it is more efficient to use var in array instead of array[var]
Code:
$ awk -F"|" 'FNR==NR{a[$1,$2]++;next}!a[$1,$2]' file1 file2


Last edited by Akshay Hegde; 12-27-2013 at 12:37 PM..
This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and compare values from different txt files

Hello, i am new in Bash. Actually i have a directory : /home/resultfiles and inside i have these txt files: 531_1.out.res, 531_2.out.res , 531_3.out.res 532_1.out.res, 532_2.out.res , 532_3.out.res 533_1.out.res, 533_2.out.res, 533_3.out.res All these txt files has this format : num_q all... (3 Replies)
Discussion started by: nimpoura
3 Replies

2. Shell Programming and Scripting

Comparing two .txt files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt Code: name1 name2 name3 file1 has to be comared with file2 defaultfile.txt Code: name1 name2 (16 Replies)
Discussion started by: draghun9
16 Replies

3. UNIX for Dummies Questions & Answers

How to tar all files except logs and .txt files

Hi I greatly appreciate the forum and the people here very helpful :) I could able to tar all the directories, sub dirs and the files. But unable to tar all the directories excluding the log and text files. Can anyone please advise the exact command to use for excluding .log and .txt? PS:... (4 Replies)
Discussion started by: Olivia
4 Replies

4. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

5. Shell Programming and Scripting

Compare two txt files

Hi, I want to compare two txt files and output the matches to a file. For example I have FILE1 - accounts1.txt Peter norman elektra table CHAIR Newman nOvice FILE2 - accounts2.txt noland trainfil peter Newman norman neverlan CHAIR jackson KelSo (4 Replies)
Discussion started by: novice_sn6
4 Replies

6. UNIX for Dummies Questions & Answers

Renaming of .txt files

Hi Guys, I am a newbie to Unix . I am going through Unix Commands. I have a issue . Please try to find a solution for this... Example: I am in my current directory now which contains a folder name 'Test' . The folder Test contains some files with .txt extension.Inside the Test... (5 Replies)
Discussion started by: mraghunandanan
5 Replies

7. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

8. Shell Programming and Scripting

txt files

How can I know if a file contain text or no? PS: I have to do a script that search for a pattern in text files (1 Reply)
Discussion started by: DNAx86
1 Replies

9. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

compare two txt files

i have two files file1 ----- absgsh jshsh shshhs hshhs file2 ------ kakkaka iurir brbrbrbr rjrbjrhjkehr rgjhergrhg hrghrgh jrhgrjg i want bash shell script to campare line1-file1 - line-file2 line2-file1 -line2 file2 ........ ( without diff , cmp , comm ) thanks... (8 Replies)
Discussion started by: space13
8 Replies
Login or Register to Ask a Question