diff lines from 2 files in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting diff lines from 2 files in a while loop
# 1  
Old 03-26-2008
diff lines from 2 files in a while loop

Okie I have two files.

file1 with input

asdf_s45
fdsa_s20
jkl_s32
lkj_s3

and file2 with input

asdf_s44
fdsa_s19
jkl_s31
lkj_s2

now I have counted the total number of lines in the file and put it in a variable so num_lines=4

now I have a while loop to repeat a diff command for each line from each file and print it out so:
x=1

while ((x<$num_lines)) ; do
sed '$xq;d' file1 > final_file1
sed '$xq;d' file2 > final_file2

read final1 < final_file1
read final2 < final_file2
diff $final1 $final2 > final_difference
((x=x+1))
done
exit 0

This will not allow $x to be a number in the sed command.. need help..
# 2  
Old 03-26-2008
Variables in single quotes do not get expanded. You need to use double quotes.
# 3  
Old 03-26-2008
okie got it, line
"sed '$xq;d' file1>final_file1"
should be
"sed "$xq;d" file1>final_file1"

now Im trying to do a diff of the variables and place it into a file
final_difference$x for each line diffed.. somehow it is not working though..is something wrong with my while loop?
# 4  
Old 03-26-2008
okie got that fixed lol.. problem was
the line was supposed to be
"sed "${x}q;d" file1>final_file1"

after that I had to change the diff line to

"diff $file1 $file2>>final_difference"

now I just need to figure out how to put a title to each new diff file to show separation. anyone?
# 5  
Old 03-26-2008
You mean unique file name for each new diff file? Why not use this precious $x variable you have anyway?
# 6  
Old 03-26-2008
actually a new start title for each diff entry (sorrie not keeping it in separate files)

so what I tried to do was

x=1

while ((x<$num_lines)) ; do
sed "${x}q;d" file1 > final_file1
sed "${x}q;d" file2 > final_file2

read final1 < final_file1
read final2 < final_file2

echo "Differences between " $final1 " and " $final2 >>final_difference
diff $final1 $final2 >> final_difference
((x=x+1))
done
exit 0


and it does add a title on the top of each difference but then gives gibberish at the end for files that did not have a diff..
# 7  
Old 03-26-2008
You could compare them or look for a zero-sized file or otherwise think of a condition to avoid that when there is no difference.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Git diff exclude swapped lines

Hi, I am doing aws security group auditing every day to find the difference. I am using git to find the difference. But some times some security group rules order is changing up and down(swapping lines). So 'git diff' command gives this as a difference which i dont want(i need only new lines... (2 Replies)
Discussion started by: jobycxa
2 Replies

3. Shell Programming and Scripting

Diff command file entries in different lines

Hello, I have two files to compare these contain some contents like this : FIle 1 : A B C E File 2 has some new entries and the old entries are in some different ordre File 2 could be like this : C E A B G I (4 Replies)
Discussion started by: ajayram
4 Replies

4. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

5. Shell Programming and Scripting

Help in replacing two blank lines with two diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. Sed "/data/{G;G;}/" filename. In the file, after data tag, two lines got inserted blank lines.. Now... (1 Reply)
Discussion started by: arjun_arippa
1 Replies

6. Shell Programming and Scripting

loop through lines and save into separate files

I have two files: file-gene_families.txt that contains 30,000 rows of 30 columns. Column 1 is the ID column and contains the Col1 Col2 Col3 ... One gene-encoded CBPs ABC 111 ... One gene-encoded CBPs ABC 222 ... One gene-encoded CBPs ABC 212 ... Two gene encoded CBPs EFC... (7 Replies)
Discussion started by: yifangt
7 Replies

7. Shell Programming and Scripting

Show entire lines with diff command

Hi, When I run the diff command using diff -yt file1 file2, I get the output in which original lines are truncated. I tried using -W switch with diff. However, that does not produce exact output as I want. Is it possible to show entire line of file1 and file2 in diff command's output? ... (8 Replies)
Discussion started by: jal_capri
8 Replies

8. Shell Programming and Scripting

counting the lines of diff files in loop

i have two file. i want to count the lines of each file one by one in loop and compare it. can any one pls help me on this? (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

9. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question