awk code to find difference in second file which is not present in first file .


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk code to find difference in second file which is not present in first file .
# 1  
Old 10-21-2019
awk code to find difference in second file which is not present in first file .

Hi All,

I want to find difference between two files and output only lines which are not present in second file .I am using awk and I am getting only the first difference but I want to get all the lines which are not present in file2 .Below is the code I am using . Please help to get the desired output .

Code:
awk ' NR==FNR { a[NR]=$0; next } $0 != a[FNR] { print a[FNR];a[FNR]++; exit }' file1.txt file2.txt

Thanks ,
Srinivas

Last edited by vgersh99; 10-21-2019 at 10:17 AM.. Reason: code tags, please!
# 2  
Old 10-21-2019
This prints any lines in file1.txt that don't appear in file2.txt:

Code:
awk ' NR==FNR { a[$0]; next } !($0 in a)' file2.txt file1.txt

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 10-22-2019
Hello Chubler_XL ,

Thanks for your prompt response . But I am getting few common lines as well with the below code i.e lines which are common to both the files but not all few of them .Your help is highly appreciated .

awk ' NR==FNR { a[$0]; next } !($0 in a)' file2.txt file1.txt

Thanks ,
Srinivas
# 4  
Old 10-22-2019
There will be a difference between the two lines. Possible different case or white space characters (tabs vrs spaces, etc.).
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-22-2019
Thanks again , do we have any way out to suppress/ignore them and find the difference .
# 6  
Old 10-22-2019
Pls show detailed, abbreviated but representative, samples of respective files, supported by e.g. a hexdump of each. so we stop shooting in the dark and start a data driven analysis.
# 7  
Old 10-22-2019
Hi Rudic ,

I am attaching two files namely file1 and file2.I have removed /proc and /opt file system in file2 . Now when I use below command it is giving me two more file system along with /proc and /opt .

awk ' NR==FNR { a[$0]; next } !($0 in a)' file2.txt file1.txt

Expected output :
/proc - - - - /proc
/dev/hd15opt 2.50 1.37 0.13 92% /opt

Actual Output :
/proc - - - - /proc
/dev/hd15opt 2.50 1.37 0.13 92% /opt
abcdef:/ghi/USER_DATA/Tech-ABC/nmon 349479.97 211239.03 138240.94 61% /nmon_data
abcdef:/ghi/ABC/NIK 349479.97 211239.03 138240.94 61% /mpc
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

2. Shell Programming and Scripting

Systemd errors of missing file “No such file or directory” inspite of file being present

The contents of my service file srvtemplate-data-i4-s1.conf is Description=test service for users After=network.target local-fs.target Type=forking RemainAfterExit=no PIDFile=/data/i4/srvt.pid LimitCORE=infinity EnvironmentFile=%I . . . WantedBy=multi-user.target (0 Replies)
Discussion started by: rupeshkp728
0 Replies

3. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

4. UNIX for Dummies Questions & Answers

Find the timestamp difference between two different colums in a file

Sample Input File 2014/10/09 CDE876172588765 00:09:45 00:10:10 200 200 11.7093 2014/10/09 CDE366134588757 01:04:34 01:04:54 210 210 9.8898 2014/10/09 CDE765172345745 03:05:46 03:06:01 100 100 10.0601 2014/10/09 ... (8 Replies)
Discussion started by: rpm120
8 Replies

5. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

6. UNIX for Dummies Questions & Answers

How to find a string that is present in the file or not?

Im pretty new to shell scripting, but i have got the jist of it over the past 10 weeks or so i have been using it. I just have one thing im stuck on thats bugging the hell out of me. Here it goes... I need a script that - You enter a filename and then a string which reports whether that string... (2 Replies)
Discussion started by: Waggie14
2 Replies

7. Shell Programming and Scripting

Find file size difference in two files using awk

Hi, Could anyone help me to solve this problem? I have two files "f1" and "f2" having 2 fields in each, a) file size and b) file name. The data are almost same in both the files except for few and new additional lines. Now, I have to find out and print the output as, the difference in the... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. Shell Programming and Scripting

find difference in file column...

Hi All, i have a file that is tab delimited. i need help to find the rows which are having same price based on the site code but some times, there are difference so i need to find only the records which are different in all site code. Dept Sec Barcode 10001 10002 10003 10004... (1 Reply)
Discussion started by: malcomex999
1 Replies

9. Shell Programming and Scripting

Script to find all the files that contain any of the words present in another file

Hi All, I am new to UNIX and shell scripts and also new to this forum. I need a script to find all the files in a directory that contain any of the strings present in another file. Please provide me the script or if you could provide pointers to any link in this forum it would be helpful.... (4 Replies)
Discussion started by: tsanthosh
4 Replies

10. Shell Programming and Scripting

To find the count of records from tables present inside a file.

hi gurus, I am having a file containing a list of tables.i want to find the count of records inside thes tables. for this i have to connect into database and i have to put the count for all the tables inside another file i used the following loop once all the tablenames are inside the file. ... (1 Reply)
Discussion started by: navojit dutta
1 Replies
Login or Register to Ask a Question