# Diff command script

**URL:** <https://community.unix.com/t/diff-command-script/338400>\
**Category:** Shell Programming and Scripting\
**Created:** [January 7, 2014, 12:43pm UTC](https://community.unix.com/t/diff-command-script/338400 "2014-01-07T12:43:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie2010](https://community.unix.com/letter_avatar/newbie2010/32/6_449c533f586939b6e5f246632a46421f.png) [@newbie2010](https://community.unix.com/u/newbie2010)\
**Post date:** [January 7, 2014, 12:43pm UTC](https://community.unix.com/t/diff-command-script/338400/1 "2014-01-07T12:43:50Z")

</div>

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

```nohighlight
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:

```nohighlight
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

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

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [January 7, 2014, 2:12pm UTC](https://community.unix.com/t/diff-command-script/338400/2 "2014-01-07T14:12:27Z")

</div>

Have you tried a loop?  
Loop over the short names: it's easy to construct the 2nd name (simply add `.z` )

```nohighlight
for f in *.txt
do
  if [-f "$f" -a -f "$f".z]
  then
    # both files exist
    diff -u "$f" "$f".z
  fi
done

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 7, 2014, 3:02pm UTC](https://community.unix.com/t/diff-command-script/338400/3 "2014-01-07T15:02:14Z")

</div>

... and I'm pretty sure that some or even most of your `sed` and `awk` pipes can be combined into one single awk command.

---

<div class="post-metadata">

**Author:** ![newbie2010](https://community.unix.com/letter_avatar/newbie2010/32/6_449c533f586939b6e5f246632a46421f.png) [@newbie2010](https://community.unix.com/u/newbie2010)\
**Post date:** [January 7, 2014, 3:06pm UTC](https://community.unix.com/t/diff-command-script/338400/4 "2014-01-07T15:06:23Z")

</div>

Thanks, made in Germany. I also found I could use sed -r and also the semicolon to combine the sed commands as Rudi mentioned!

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 7, 2014, 4:28pm UTC](https://community.unix.com/t/diff-command-script/338400/5 "2014-01-07T16:28:43Z")

</div>

Try to use the sub() or gsub() functions in `awk` to achieve what you are using the `sed` command for.
