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
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.