Link to home
Start Free TrialLog in
Avatar of DOCDGA
DOCDGA

asked on

Compare linux 2 files

I am learning linux commands and I would like to receive some help comparing to files.

I would like to compare 2 files and output any difference in the second file.

File A                            File B

411;A;A;1                     411;A;A;2
411;A;B;2                     411;A;B;3
411;B;A;1                     411;B;A;2
411;B;B;2                     411;B;B;2
                                     411;C;A;1
                                     411;C;B;1

I would like to compare the files based on column 2 in File A. If File B has any thing different in column 2 I would like to output it to a file.
Avatar of arnold
arnold
Flag of United States of America image

Comm -1 fileA fileB
http://unixhelp.ed.ac.uk/CGI/man-cgi?comm

The -1 will suppress items unique to FileA.
"comm" and "diff" work on whole lines.

Since you wish to compare based on columns try this:

awk -F";" 'BEGIN {while(getline < "fileA") A[$2]="Y"} ; {if(A[$2]!="Y") print}' fileB > fileC

The above writes the whole content of the concerned lines to fileC
To write just the second columns use:

awk -F";" 'BEGIN {while(getline < "fileA") A[$2]="Y"} ; {if(A[$2]!="Y") print $2}' fileB > fileC
try installing vimdiff
Avatar of DOCDGA
DOCDGA

ASKER

woolmilkporc, I have tried your solution but nothing happens. My terminal is not responded.

Any suggestion?
Which was the exact command you used?

What are your filenames, and are the columns inside indeed separated by semicolons?

With your sample files I'm getting this output:

411;C;A;1
411;C;B;1
Avatar of DOCDGA

ASKER

I used the following command:
awk -F";" 'BEGIN {while(getline < "fileA") A[$2]="Y"} ; {if(A[$2]!="Y") print}' fileB > fileC

My file names are fileA and fileB.
Yes, and you don't see anything on your terminal because you requested that you >> would like to output it to a file <<

Please look into fileC and you'll see.
Avatar of DOCDGA

ASKER

I understand I that I wouldn't see anything on the terminal but I'm not seeing a file in my current directory.
Avatar of DOCDGA

ASKER

Okay, I was just able to search and find fileC but it is blank. I was expecting the same results you had.
Me too. Did you use files with the content exactly as posted?

And fileC should indeed get placed in your current directory and you should not have to search for it.
Avatar of DOCDGA

ASKER

Yes, I used the same exampe text.

I deleted the blank fileC.

I just copied the awk command from the forum then paste it in my terminal and hit enter.
awk -F";" 'BEGIN {while(getline < "fileA") A[$2]="Y"} ; {if(A[$2]!="Y") print $2}' fileB > fileC


Now it just sitting with a blinking cursor.

I don't see a new fileC and my terminal seems to be waiting on me to enter something.
Perhaps you should put it into a file filename (maybe even typing it by hand) to then run

sh filename

Since everything works for me I can only think of some copy-and-paste (character set?) issue.

awk sits and waits when there's no input fie specified at the end (not inside the BEGIN section, omitting the filename there would throw an error).

You don't see something like

+1>

do you?
Avatar of DOCDGA

ASKER

I don't see +1>.

I have tried typing out the command by hand but when I press enter. My cursor just sits at the end of the line.

[rwilliams@rwilliams ~]$ awk -F";" 'BEGIN{while(getline < "fileA")A[$2]="Y"};{if(A[$2]!="Y")print$2}'fileB > filename
^C          

I press Ctrl + C to stop command and bring back up my prompt.

It does not bring up my [rwilliams@rwilliams ~]$
Avatar of DOCDGA

ASKER

Okay, I feel silly about not being able to locate the file. I was going back to my directory with the gui and forgot to refresh. But my output files are blank.
There must be a space between the closing single quote and the filname "fileB", otherwise awk will not know that it's a filename and will wait for input data!

awk -F";" 'BEGIN{while(getline < "fileA")A[$2]="Y"};{if(A[$2]!="Y")print$2}' fileB > filename

While testing you can omit the final redirection "> filename", of course!
Avatar of DOCDGA

ASKER

I remove the space but the file is blank. I don't understand why.
Do not remove the space,  add it.
Avatar of DOCDGA

ASKER

I'm sorry I added the space between print$}_fileB

My output file is still blank
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DOCDGA

ASKER

Thanks, your the best.

So is it  best practice to add spaces?
Depends. Basically it's much better, but if you don't have a script and want to type everything  on the command line instead then saving a bit of space might be desirable.

Does it work now?
Avatar of DOCDGA

ASKER

Yes, it works now.

I will play with and try to modify it so it can monitor column 2 & 3 in fileA and compare it with fileB.

Thanks
Avatar of DOCDGA

ASKER

woolmilkporc is very patient and explained things so that I could learn something new.