Diff command script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff command script
# 1  
Old 01-07-2014
Diff command script

I am trying to use the diff command on multiple files. I have a script like this:

Code:
diff -u <(head -n99999999 /tmp/*txt ) <(head -n99999999 *txt.z) |sed -e '/\-\//s/\-/deleting   /g'  -e '/\+\//s/\+/rearranging  /g' -e '/=\//s/\.*//g' -e '/txt.z/d' -e '/@/d' |gawk '$1 ~ /^deleting$/ || $1 ~ /^rearranging$/ || $1 ~ /==/ {print}' |gawk '/[txt]/{x="F"++i;}{print > x;}'

The script works and creates F* files but the problem is that some items are marked as "deleting" which should not be. I found that when I run one file at a time it works fine:

Code:
diff -u <(head -n99999999 /tmp/product-txt ) <(head -n99999999 /tmp/product-txt.z)

I am wondering how to feed two files into the diff command at a time. I have tried multiple commands, such as paste (but I have over 12 files) and also tried using
Code:
sed -n "$count"p

in a script without success. I am sure that there is a read "script" that could be used with diff that would do it but I can't figure it out.

I really just need two files, one from txt and one from txt.z read into the diff command one at a time to compare the differences. Then the script should move on to the next pair. Ordinary diff doesn't work it will just run a check on 2 files and that's it. That is why I was using head -n999999.
# 2  
Old 01-07-2014
Have you tried a loop?
Loop over the short names: it's easy to construct the 2nd name (simply add .z)
Code:
for f in *.txt
do
  if [ -f "$f" -a -f "$f".z ]
  then
    # both files exist
    diff -u "$f" "$f".z
  fi
done

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 01-07-2014
... and I'm pretty sure that some or even most of your sed and awk pipes can be combined into one single awk command.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-07-2014
This worked great!

Thanks, made in Germany. I also found I could use sed -r and also the semicolon to combine the sed commands as Rudi mentioned!
# 5  
Old 01-07-2014
Try to use the sub() or gsub() functions in awk to achieve what you are using the sed command for.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with diff command

Platform :Oracle Linux 6.4 Shell : bash In the below sample, although the lines in a.txt and b.txt are jumbled up, there is only one difference : b.txt has an extra line NETHERLANDS $ cat a.txt SPAIN NORTH KOREA PORTUGAL GERMANY SYRIA $ $ $ cat b.txt GERMANY NORTH KOREA SPAIN... (6 Replies)
Discussion started by: John K
6 Replies

2. Shell Programming and Scripting

One-way diff command?

Hello, I am trying to find the different files between multiple directories in Linux, here is a small assumption of what is inside the directories dir1 dir2 dir3 1.txt 1.txt 1.txt 2.txt 3.txt 3.txt 5.txt 4.txt 5.txt 6.txt 7.txt 8.txt I am using the following... (4 Replies)
Discussion started by: Error404
4 Replies

3. UNIX for Dummies Questions & Answers

Re:using the diff command

Hi Guys I have a situation where I would like to use the diff command but I would like to see "number" of differences and than send it through and if statement and than view the difference if greater than 1. Eg. diff file1 file2 > than gives the "number" and I than say - if number >1... (3 Replies)
Discussion started by: Prega
3 Replies

4. Shell Programming and Scripting

Process diff command output in a shell script

diff -yta file1 file2 #!/usr/abc/b/bin/perl5.6 | #!/usr/abc/b/bin/perl5.8 Notable thing about above line is "|" appears at 62nd position. When the same line is assigned in a variable in a ksh script, using ss=$(diff -yta file1 file2) it appears as ... (4 Replies)
Discussion started by: bhaliyajalpesh
4 Replies

5. Shell Programming and Scripting

diff command help

Hi all diff file1 file 2 command will give us op of diff between two file. But it aslo give its position and sign "<" or ">". I dont want position and sign in op. Only diff of content should be come as op. Kindly help me for this. Regards Jaydeep (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

6. UNIX for Dummies Questions & Answers

diff command

Is there any option for the diff command (or maybe an entirely different command) that will give you only the text that differs between two files? When I use diff file1 file2, if any text on that line differs from one file to the next it'll print out the entire line. I'd like to see only the text... (2 Replies)
Discussion started by: red baron
2 Replies

7. AIX

diff command

hello i've two files. how i get the diff between the two files to new file. thanks best regards ariec (3 Replies)
Discussion started by: ariec
3 Replies

8. Shell Programming and Scripting

need help in diff command :

i have 2 file named test1,test2 contents of test1: 1 2 3 --------------------------- contents of test2: 1 2 3 4 5 -------------------------------------------------------- my desired o/p should be: diff test2 test1 4 (5 Replies)
Discussion started by: ali560045
5 Replies

9. Shell Programming and Scripting

diff command

All, How to exclude a directory while diff execution? For ex: To exclude file which we don't want to see diff, we have -x <filename>. Thanks in advance (1 Reply)
Discussion started by: Vichu
1 Replies

10. UNIX for Dummies Questions & Answers

diff command

Hi, I have 2 files i would like to have a DIFF command: 1.Marks differences between files or 2.Mentions just the differences Thanks :) (7 Replies)
Discussion started by: gilead29
7 Replies
Login or Register to Ask a Question