Diff between 2 files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff between 2 files using awk
# 1  
Old 11-30-2009
Diff between 2 files using awk

Hi Experts,

Could you please help me to find the difference between two files. I tried the diff command but did not like the output as it contained < and > signs and the line numbers. Is it possible to do something using awk?

I have two files, say File1.txt contains 5 values and File2.txt contains only 2 values of what File1.txt contains. I want a command or script that will list me the 3 missing values from File2.txt. Here are the examples

File1.txt
034J
025J-01
045k
089G-02
04J01

File2.txt
025J-01
04J01

Output.txt
034J
045k
089g-02

Any help is highly appreciated.

Thanks in advance
# 2  
Old 11-30-2009
Easier with grep:

Code:
grep -vf file2 file1
034J
045k
089G-02

Or if you still want to use awk:

Code:
awk 'NR == FNR { A[$0]=1; next } !A[$0]' file2 file1
034J
045k
089G-02


Last edited by Scott; 11-30-2009 at 08:52 AM.. Reason: typo
# 3  
Old 11-30-2009
or diff... 'man diff ' , I'm pretty sure there's a way to only output differences.
EDIT: probably not so useful...even
$diff --suppress-common-lines file1 file2
still outputs the line numbers as well.

Last edited by gcampton; 11-30-2009 at 10:04 AM..
# 4  
Old 11-30-2009
Diff between 2 files using awk

Hi Scottn,

Many thanks for a quick response. I tried both you solutions.

with the grep I get the following message:

grep: illegal option -- f
usage: grep -hblcnsviw pattern file . . .

with the awk script i get
awk: syntax error near line 1
awk: bailing out near line 1

I used the commands exactly the way shown (obeviously replacing with the actual file names)

could you spot the mistake here please!!

Thanks again
# 5  
Old 11-30-2009
Ah, solaris?

Use nawk or /usr/xpg4/bin/awk.
# 6  
Old 11-30-2009
Two get only the entries found in file two you could use: -

Code:
diff -s <(sort file1.txt) <(sort file2.txt) | nawk ' /</{print $2}'

# 7  
Old 12-01-2009
Hi,
Any solution to this problem?
I have the same doubt

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

3. UNIX for Dummies Questions & Answers

diff then awk two files

Hi friends, i am trying to diff two files and the result will be passed to awk and this will get the first 20 characters in every line and put the result in a file.... but i can't generate an output. $ cat file1.txt 1 2 3 4 5 $ cat file2.txt 1 2 3 4 the line of command is: (2 Replies)
Discussion started by: kokoro
2 Replies

4. Shell Programming and Scripting

Show the diff in two files using awk

Hi, How can i use AWK or any other commands to find the difference between 2 files. File A aaa bbb ccc 111 222 File B aaa ccc 111 Output bbb 222 (6 Replies)
Discussion started by: gambit97
6 Replies

5. Shell Programming and Scripting

.procmailrc and uudeview (put attachments from diff senders to diff folders)

Moderator, please, delete this topic (1 Reply)
Discussion started by: optik77
1 Replies

6. Shell Programming and Scripting

diff bw two files

Hi All, I have two files which look as below File1 serial="1" name="abc" type="employee" field="IT" serial="2" name="cde" type="intern" field="Marketing" serial="3" name="pqr" type="contractor" field="IT" serial="4" name="xyz" type="employee" field="Sales" File2 serial="1"... (3 Replies)
Discussion started by: grajp002
3 Replies

7. Shell Programming and Scripting

diff of files

Hi, I have 2 files.I want to check if file1 is contained in file2. A.txt: ----- AAA BBB B.txt: ------ CCC AAA BBB DDD I want to check if A.txt is contained in B.txt. Can it be done using SED ? (12 Replies)
Discussion started by: giri_luck
12 Replies

8. Shell Programming and Scripting

Diff b/w 2 files

Hi Masters, I have two files named file1 and file2. Both the files contains the same contents with some difference in comments,space.But no content change. I tried to find the diff between the two files to make sure that contents are same. For that i tried diff -ibw file1 file2 But... (1 Reply)
Discussion started by: ecearund
1 Replies

9. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question