Awk program to print output of comm command in required format

5 pts.
Tags:
Awk
Linux
Linux Commands
I have two files x and y containing two fields emp number and emp name in each file. I want to bring out the mismatching entries and missing entries into a file. File x has the correct emp number and emp name. y has correct emp number but name is not correct or blank or correct.  I used comm -13 x y > z1 and comm -13 x y >z2. The out put files now consists of unique fields in file x and file y. But i want the print output  as emp number , emp name in file1 , emp name in file2 for comparing the emp names. The x and y files has more than 50000 lines. I heard that through awk program we can do that but i couldn't achieve it. I could print either file z or file z1. but not combined output as required. I would be thankful if you guide me at the earliest. Thankyou sagar

Software/Hardware used:
linux, awk programs

Answer Wiki

Thanks. We'll let you know when a new response is added.

Why don’t use diff command?

First, sort both files (I hope these are plain text):

sort x > x_sorted
sort y > y_sorted

Then, diff sorted files:

diff -wB x_sorted y_sorted

-wB means ignore all blanks and blank lines in comparison.

If it is necessary to double, etc. spaces and tabs and blank lines, use a variation:

grep -v ‘^$’ x | sed -r ‘s/[[:space:]]+/ /g’ | sort > x_sorted
grep -v ‘^$’ y | sed -r ‘s/[[:space:]]+/ /g’ | sort > y_sorted

diff x_sorted y_sorted

Good luck

Petko

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • brydell
    Just very naievly assuming that the records in your two files z1 and z2 are in the same order, you can use 'getline < "filename"' to read from a second file. Again, with no testing, just displaying the content of the lines, try this: BEGIN { num=" " ; nam1=" "; nam2=" " }{  num = $1 ; nam1 = $2 ;  getline < "file_z2" ; nam2 = $2 ;  print num, ",  ", nam1,",  ",nam2}END {} If you need to check that the two read lines actually contain the same emp-num, it of course gets trickier.... Good Luck. 
    560 pointsBadges:
    report
  • mikejava
    up
    35 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following