File Comparison: Print Lines not present in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Comparison: Print Lines not present in another file
# 1  
Old 09-27-2012
CPU & Memory File Comparison: Print Lines not present in another file

Hi,
I have fileA.txt like this.
Code:
B01B02	D0011718
B01B03	D0012540
B01B04	D0006145
B01B05	D0004815
B01B06	D0012069
B01B07	D0004064
B01B08	D0011988
B01B09	D0012071
B01B10	D0005596
B01B11	D0011351
B01B12	D0004814
B01C01	D0011804

I want to compare this against another file (fileB.txt) that has a subset of the lines in fileA.txt.
Code:
B01B02	D0011718
B01B03	D0012540
B01B07	D0004064
B01B08	D0011988
B01B09	D0012071
B01B10	D0005596
B01B11	D0011351

The output needs to have only the difference between the two files.
Output fileC.txt
Code:
B01B04	D0006145
B01B05	D0004815
B01B06	D0012069
B01B12	D0004814
B01C01	D0011804

Awk preferred, but can also do with python.
Thanks
~GH
# 2  
Old 09-27-2012
Try:
Code:
awk 'NR==FNR {d[$0];next} ! ($0 in d) {print}' fileB.txt fileA.txt

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-27-2012
If the files are sorted, as they are in your sample data, then comm can do the job.

Regards,
Alister
# 4  
Old 09-28-2012
The awk command worked fine..
For some reason neither comm or diff -a --suppress-common-lines worked for me.. not sure why.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines present in first that match second variable. And ones that does not exist in second.

I have multi line input(var1) and reference(var2) variables. How to capture lines not present in var2 but present in var1? How to capture lines present var2 but not in var1? # configuration from server var1=""" Custom JAX-RS Custom Shared Web 2.0 """ # required configuration... (6 Replies)
Discussion started by: kchinnam
6 Replies

2. UNIX for Dummies Questions & Answers

Adding variable value in the begining of all lines present in a file.

I am getting the varible value from a grep command as: var=$(grep "Group" File1.txt | sed 's/Group Name*//g;s/,//g;s/://g;s/-//g') which leaves me the value of $var=xyz. now i want to append $var value in the begining of all the lines present in the file. Can u please suggest? Input file: 1... (10 Replies)
Discussion started by: rade777
10 Replies

3. Shell Programming and Scripting

awk file comparison, x lines after matching as output

Hello, I couldn't find anything on the Forum that would help me to solve this problem. Could any body help me process below data using awk? I have got two files: file1: Worker1: Thomas Position: Manager Department: Sales Salary: $5,000 Worker2: Jason Position: ... (5 Replies)
Discussion started by: killerbee
5 Replies

4. UNIX for Advanced & Expert Users

query display number lines or records present in file only numeric value -without filename

Hi all Thanks in advance........... Please help me for this issue............ I have a file it has 11 records . I used the command like .... >$ wc -l file 11 file I'm getting output like 11 file (no.of records along with filename) here my requirement is, I want to display only... (3 Replies)
Discussion started by: ksrivani
3 Replies

5. UNIX for Dummies Questions & Answers

To get the lines that are not present in file

Hi I have got two files File1: Row1 Row2 Row3 Row4 File2: Row3 Row4 Now my requirement is search each and every line of file1 in file2 and if the record do not exist in file2 then write that to an output file. Output file should be as below Row1 Row2 (4 Replies)
Discussion started by: sbhuvana20
4 Replies

6. Shell Programming and Scripting

numbers comparison in fields of a file and print least value of them

Hi , I'm trying to compare fields in the file, I want compare the numbers in each column and get the least value of it. > cat input_file 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.2050 -0.6629 -0.6407 -0.6599 -0.4085 -0.3959 -0.2526 -0.3597 0.3439 0.2275 0.2780 ... (5 Replies)
Discussion started by: novice_man
5 Replies

7. Shell Programming and Scripting

Using SED to print the links present in a file

i was trying to print the links present in a file using SED. sed -n 's/.*\(\(http\|ftp\|https\):\/\/\(*\).\(net\|html\)\).*/\1/gp' example content of example file is The URL for my site is: http://mysite.com/mydoc.html. You might also enjoy ftp://yoursite.com/index.net for a good... (5 Replies)
Discussion started by: Kesavan
5 Replies

8. UNIX for Dummies Questions & Answers

Extracting lines present in one file but missing in another using Perl

Hey I have an input file containing a list of numbers like: U01120.CDS.1 D25328.CDS.1 X15573.CDS.1 K03515.CDS.1 L44140.CDS.10 U24183.CDS.1 M97347.CDS.1 U05259.CDS.1 And another input file containing results created on the basis of the above input: G6PT_HUMAN U01120.CDS.1 -1.9450 3.1706... (1 Reply)
Discussion started by: Banni
1 Replies

9. Shell Programming and Scripting

String comparison: All lines in file but each within line

Hi folks, I'm trying to do a bit of extra monitoring on who is sending mail through our server and as I'm sure you'll understand, the log files aren't exactly small. As a note, I'm working to sort out a solution to this myself, so I'll keep editing the post when I get a chance. Here an example... (2 Replies)
Discussion started by: beddo
2 Replies

10. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies
Login or Register to Ask a Question