compare two files using while and sed .. debug my script please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two files using while and sed .. debug my script please
# 1  
Old 09-25-2008
compare two files using while and sed .. debug my script please

Hi, I'm a newbie to Linux. I have not done programming before, but I accidentally stumble upon Linux scripts at work about 2 weeks ago. I got interested and write scripts to automate my job duties.

I need to write a script to compare 2 files (very long list) side by side so it's easier to spot the differences. Here's the objective:

File1
-----
die1
die2
die3
lb_name1
lb_name2
lb_name3

File2
----
die2
lb_name1
lb_name2

Desire output
-------------
die1
die2 die2
die3
lb_name1 lb_name1
lb_name2 lb_name2
lb_name3


--------------------Here's my script--------------------
#!/bin/bash

# Compare two files and show the difference

awk -F';' 'BEGIN{while(getline<"file1") a[$1]=2};a[$1]!=2' file2 > listdifference

cksum file1 | awk '{print$1}' > x
cksum file2 | awk '{print$1}' > y
x=`cat x`
y=`cat y`

function test
{
if [ $x -le $y ]
then
echo "First file x is smaller. Add difference into smaller file x and remove the difference."

paste -d"\n" listdifference file1 | sort | uniq > lists
awk NF lists > listfinal

while read difference
do
echo "Deleting line $difference"
#sed "/$difference/d" listfinal > output
sed -e "s/$difference//g" listfinal > output
done < listdifference
else
echo "Second file y is bigger. Add line into x."
fi
}
test
--------------------End script---------------------



Most of these commands I found here in this forum. Thanks to everyone who post these commands. Also, I google some of the commands and self taught.

The above awk command compares file1 and file2 and gives me their differences in listdifference.

Then I write a loop just to remove the differences using sed and leave the blank space. Save the result to a file call output.

output (wanting to achieve)
------

die2

lb_name1
lb_name2



When I run my lenthy script, the output looks like below, which is not what the loop sed should have done:

output (result after script is ran... doesn't look right)
------
die1
die2
die3
lb_name1
lb_name2


The sed in the loop should have removed each individual lines that are not the same (listdifference) and leave a blank. But it only do this to the last line. I've tried other ways to sed -f , sed -e ' -e , and cmd=$cmd, etc ... but it still doesn't look right.

Once I have the output, then I can combine file1 and output using the following command to achive the desire output.

paste file1 output | awk -F '\t' '{printf "%-32s%s\n", $1, $2}' > desire_output

I'm getting close. I'm debugging and learning at the same time. Please help me to debug or change my command synosis Smilie

I know there are experts in this forum that can write this using 1 or just a few lines. If just one line, then please break it down and explain so I can study from your script. This is the first time I post in any forum. I hope to be able to navigate back to this thread.

Thank you very much.

Last edited by shamushamu; 09-25-2008 at 07:27 PM..
# 2  
Old 09-25-2008
I might be crazy but...

You show two examples of expected results in your post. I am kind of confused as to which you are looking for. Your first expected output shows two files side by side and the second one shows the output being only what is the same in the file.

But by reading the whole thing it appears that your end goal is to add file2's unique contents to file1, making one file with all of the content. But there are a few assumptions here that I think may hurt you, but before I get into those we need to know the goal.

I am not trying to bash you at all, if you just learned scripting and are already into sed and awk you are way ahead of where I was at 2 weeks, but what exactly are you looking for?

Last edited by ctruhn; 09-25-2008 at 08:42 PM..
# 3  
Old 09-25-2008
Hi ctruhn, Thank you for replying so quickly. I was afraid that my post is lengthy and confusing. Smilie

My goal is to compare 2 different files and show the result side by side in this format:

Desire output of file1 and file2 separated by a tab
---------------
die1
die2 die2
die3
lb_name1 lb_name1
lb_name2 lb_name2
lb_name3

There should be a tab between the 2 column. (This forum will not let me use tabs .. it turns into spaces Smilie). So when we glance at this output, we will quickly see that the differences between the 2 files are die1, die3, and lb_name3. I've been researching in this post and googling for the past week and have not found a solution to this problem.

Please let me know if there is an easier way than what I proposed in my previous post.

Thanks. Smilie

Last edited by shamushamu; 09-25-2008 at 09:12 PM..
# 4  
Old 09-25-2008
This may be a bit obvious, but if you want to see the differences couldn't you just use diff?
# 5  
Old 09-26-2008
You may try using sdiff,

sdiff file1 file2
die1 <
die2 die2
die3 <
lb_name1 lb_name1
lb_name2 lb_name2
lb_name3 <


-TCOK
# 6  
Old 09-26-2008
see the man on sdiff

man sdiff
sdiff -s -w 200 file1 file2
is good
see if you have these
which tkdiff
which xdiff

if you have tkdiff or xdiff on your system you are in luck, other wise sdiff and diff will do most it the old school way...
# 7  
Old 09-26-2008
Thank you to everyone who have replied Smilie but ....

Sometimes this might be a long list, unalphabetized, and the words might be lb_nhb_5x_gihsd2_frnas, so spotting the differences between 2 columns is not practical. Yes, I have tried a lot of simple commands like paste, sort, uniq, and bdiff, but we would really prefer the following format for spotting the differences quicker and easier:

Desire Output:
File1 Files
-----------
die1
die2 die2
die3
lb_name1 lb_name1
lb_name2 lb_name2
lb_name3

Each of these columns is in a different file. I'm simply wanting to combine the 2 files, but have the same item in rows. The differences should be blanks. Even the paste -d \n command doesn't do this.

I've tried to type in the tabs between these 2 columns, but this forum will not let me use tab. With the tabs, it's really easy to spot the differences. I've been struggling with this problem for a week now. It's fun scripting but I've hit a wall, so I really need your help Smilie

Last edited by shamushamu; 09-26-2008 at 02:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to extract/compare from two files.

I have two files : Alpha and Beta. The files are as follows (without arrow marks.) Alpha: A 1 D 90 G 11 B 24 C 15 Beta: B 24 C 0 <-- G 11 D 20 <-- A 4 <-- E 777 <-- Expected output of the script : Alpha: (2 Replies)
Discussion started by: linuxadmin
2 Replies

2. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

3. UNIX for Dummies Questions & Answers

Unix Script to compare two files

Hello, I have a dat file nctilllist.dat which will be present in the directory path "/usr/lpp/web-data/mfg/nct/file-data/nctilllist.dat" nctillist.dat will have reference to files like DP100001.jpg,DP10002.PDF,DP100003.doc on the path /usr/lpp/web-data/mfg/nct/file-data will have... (12 Replies)
Discussion started by: gayathrivm
12 Replies

4. Shell Programming and Scripting

Compare two files using shell script

Hi i want to compare two files and i need the o/p of only difference here the files file1 achilles aedxbepo aedxbwdm01 aedxbwdm02 albedo amarice ambrister anakin anton argon artephius asgard avatar aymara (10 Replies)
Discussion started by: venikathir
10 Replies

5. Shell Programming and Scripting

Shell Script to Compare Two Files

I have a directory with about 6 files that we receive regularly. these 6 files contain information for 3 different units, 2 for each unit. files related to a specific unit are named similarly with a change in number at the end of the file. the numbers should be sequential. for each grouping of... (3 Replies)
Discussion started by: scriptman237
3 Replies

6. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

7. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

8. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies
Login or Register to Ask a Question