Solved

Compare linux 2 files

Posted on 2013-06-12
23
488 Views
Last Modified: 2013-06-13
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.
0
Comment
Question by:DOCDGA
23 Comments
 
LVL 77

Expert Comment

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

The -1 will suppress items unique to FileA.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39243681
"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
0
 
LVL 8

Expert Comment

by:morsun
ID: 39243745
try installing vimdiff
0
 

Author Comment

by:DOCDGA
ID: 39244706
woolmilkporc, I have tried your solution but nothing happens. My terminal is not responded.

Any suggestion?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39244713
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
0
 

Author Comment

by:DOCDGA
ID: 39244749
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39244771
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.
0
 

Author Comment

by:DOCDGA
ID: 39244831
I understand I that I wouldn't see anything on the terminal but I'm not seeing a file in my current directory.
0
 

Author Comment

by:DOCDGA
ID: 39244840
Okay, I was just able to search and find fileC but it is blank. I was expecting the same results you had.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39244858
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.
0
 

Author Comment

by:DOCDGA
ID: 39244874
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.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39244910
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?
0
 

Author Comment

by:DOCDGA
ID: 39244949
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 ~]$
0
 

Author Comment

by:DOCDGA
ID: 39244973
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39244979
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!
0
 

Author Comment

by:DOCDGA
ID: 39245030
I remove the space but the file is blank. I don't understand why.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39245169
Do not remove the space,  add it.
0
 

Author Comment

by:DOCDGA
ID: 39245178
I'm sorry I added the space between print$}_fileB

My output file is still blank
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39245197
Well, now you're missing the closing single quote after }.

And what I didn't see at first sight - there must be a space between print and $2 , too.

Best add spaces between all items. Most of them are not needed, but with them in place we can see better what we're doing:

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

Author Comment

by:DOCDGA
ID: 39245211
Thanks, your the best.

So is it  best practice to add spaces?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39245230
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?
0
 

Author Comment

by:DOCDGA
ID: 39245238
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
0
 

Author Closing Comment

by:DOCDGA
ID: 39245241
woolmilkporc is very patient and explained things so that I could learn something new.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ubuntu 16.04 AMD video card driver issues 20 341
Issue to mail 11 72
php ssh2_scp_send 1 49
AWS - HAProxy- KeepAlived 5 17
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now