Scan two files and print values missing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scan two files and print values missing
# 1  
Old 06-30-2010
Power Scan two files and print values missing

Dear Experts,

Have been seraching this forum from this morning for my query but dint find hence posting it her... Basically i have two input files BSS and MSS which has a unique string , hence i hav tried and seperated the text to compare frm both files .. Any my present input files look like below:

HTML Code:
 
File1
 
40468006A2027 
40468006A2028 
40468006A2029 
40468006A7577 
40468006A7578 
40468006A7579
HTML Code:
 
File2
40468006A210D
40468006A210E
40468006A210F
40468006A7577
40468006A7578
40468006A7579
40468111222
40468abcabd
 
 
 
Here each line of file1 should be scanned with File2 , similarly vice versa and my output should be

HTML Code:
 
Data Missing in File2 which exist in File1:
40468006A2027 
40468006A2028 
40468006A2029 
Data Missing in File1 which is present in File2:
40468006A210D
40468006A210E
40468006A210F
40468111222
40468abcabd
 
Tried comm grep diff , nothing basically works as loop is required to search each value against all lines... Please help me in this regard

Thanks in Advance
# 2  
Old 06-30-2010
Code:
awk 'BEGIN{print "Data Missing in "ARGV[1]" but present in "ARGV[2]}NR==FNR{a[$0]++}!($0 in a)' File1 File2

Code:
awk 'BEGIN{print "Data Missing in "ARGV[1]" but present in "ARGV[2]}NR==FNR{a[$0]++}!($0 in a)' File2 File1

# 3  
Old 06-30-2010
Or with one command:
Code:
awk 'BEGIN{print "Data Missing in File1 which exist in File2: "}
NR==FNR{arr[$0]=$0; next}
$0 in arr {delete arr[$0]; next} {print}
END{print "Data Missing in File2 which is present in File1: "
for( i in arr ){print arr[i]}}
' file1 file2

# 4  
Old 06-30-2010
With a slightly different output:

Code:
comm -3 <(sort file1) <(sort file2)

# 5  
Old 06-30-2010
Thanks for the response..
But both the codes giving just print of both files not the difference


Code:
Data Missing in File1 which exist in File2: 
40468006A2027 
40468006A2028 
40468006A2029 
40468006A7577 
40468006A7578 
40468006A7579 
Data Missing in File2 which is present in File1: 
40468abcabd
40468111222
40468006A210D
40468006A210E
40468006A210F
40468006A7577
40468006A7578
40468006A7579


Last edited by radoulov; 06-30-2010 at 09:27 AM.. Reason: Added code tags.
# 6  
Old 06-30-2010
Do you have a space in some lines at the end of the line? In that case you can try this:
Code:
awk 'BEGIN{print "Data Missing in File1 which exist in File2: "}
NR==FNR{arr[$1]=$1; next}
$1 in arr {delete arr[$1]; next} {print}
END{print "Data Missing in File2 which is present in File1: "
for( i in arr ){print arr[i]}}
' file1 file2

# 7  
Old 06-30-2010
I am really sorry.. One file had space hence difference was showing for all records as space also it was considering.. Now working fine... Thanks a lot

Code:
Data Missing in File1 which exist in File2: 
40468006A2027 
40468006A2028 
40468006A2029 
Data Missing in File2 which is present in File1: 
40468abcabd
40468111222
40468006A210D
40468006A210E
40468006A210F


Last edited by vgersh99; 06-30-2010 at 09:44 AM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fill in missing values

Hi, I have a data sample as shown below. I want to fill in the left column so that the line will be continuous. For example, between 1 and 5 should be 2,3,4. And corresponding values in the right column will be 0. Thus the expected data should look like that: 1 1 1 10 1 2 1 3 1 5 1 6 2 0... (6 Replies)
Discussion started by: theanh0508
6 Replies

2. Shell Programming and Scripting

Get both common and missing values from multiple files

Hi, I have 5 files with two columns. I need to merge all the 5 files based on column 1. If any of them are missing then corresponding 2nd column should be substituted by missing value. I know hoe to do this for 2 files. but how can I implement for 5 files. I tried this based on 5 files but it... (2 Replies)
Discussion started by: Diya123
2 Replies

3. Shell Programming and Scripting

Insert missing values

Hi, please help with this, I need to insert missing values into a matrix for a regression analysis. I have made up an example. The first three columns are variables with levels and the next 3 are values, the 4th column missing values should be replaced by 0s, and 5th and 6th column missing... (3 Replies)
Discussion started by: ritakadm
3 Replies

4. UNIX for Dummies Questions & Answers

Merging two text files by a column and filling in the missing values

Hi, I have to text files that I want to merge by the first column. The values in the first column pretty much match for the first part. However there are some values that are present in column 1 and not present in column 2 or vice versa. For such values I would like to substitute X for the... (9 Replies)
Discussion started by: evelibertine
9 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

print out missing files in a sequence

Hello all, I have several directories with a sequence of files like this IM-0001-0001.dcm IM-0001-0002.dcm IM-0001-0003.dcm IM-0001-0004.dcm IM-0001-0005.dcm I would like to print out the name of the file that is missing. I currently have the following ineffecient way to do this... (4 Replies)
Discussion started by: avatar_007
4 Replies

7. Shell Programming and Scripting

Fill missing values with 2

Hi All, I have 100 .txt files which look like this: 3 4 5 6 7 Now, some files have some numbers missing in them and they look like this: 4 5 6 (6 Replies)
Discussion started by: shoaibjameel123
6 Replies

8. Shell Programming and Scripting

Help fixing awk code to print values from 2 files

Hi everyone, Please help on this: I have file1: <file title="Title 1 and 2"> <report> <title>Title 1</title> <number>No. 1234</number> <address>Address 1</address> <date>October 07, 2009</date> <description>Some text</description> </report> ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies
Login or Register to Ask a Question