# Comparing files lines with shell script



## bigb89 (Apr 21, 2014)

Hello guys,

I have two files (file1 and file2):

file1 has the following contents (each separated by a line):

----
100
300
500
----

file2 has the following contents:

-----
200
100
400
-----

I've created a 'for loop' that goes through and displays each line of those files (easy part). Now here's the part that I'm having some issues in doing:

I would like for each line in those files to get compared with each other.  So the script will run and display the first line of file1 (100), and then the script will compare the first line of file1 with the first line of file2 (200). Then it would print out which line has the greater value. 

So in my case above, the comparison would be 'if 100 < 200', do 'blah'.  I know how to run if statements, but I'm just having a hard time on how I can compare line by and display the results. Both files have the same amount of lines (all of the lines are just an integer), so the script would stop once it compared each line between both files. I'm sure there's a solution somewhere in google, however I just don't know what search criteria I should use, and therefore I have not found anything that would help.

Any help on this would be appreciated.


----------



## worldi (Apr 22, 2014)

The following script seems to solve your problem:


```
#! /bin/sh
set -o errexit
set -o nounset

#  This script reads two files that are both assumed to contain a single
#  integer value per line. It compares the files line by line and for each
#  lines prints the greater of the two integers.


usage() {
  printf "Usage: %s file1 file2\n" "$(basename -- "$0")"
  exit 64
}


###  Check arguments

: ${1:?$(usage)}; [ ! -f "$1" ] && usage
: ${2:?$(usage)}; [ ! -f "$2" ] && usage


###  Open the two files and assign them to file descriptors 4 and 5

exec 4< "$1"
exec 5< "$2"


###  Compare both files line by line and print the greater of the two integers
###  The loop exits as soon as EOF or an empty line is detected

read val1 <&4
read val2 <&5

while [ -n "$val1" -a -n "$val2" ]
do
  if [ "$val1" -gt "$val2" ]
    then echo "$val1"
    else echo "$val2"
  fi
  read val1 <&4
  read val2 <&5
done


###  Close files

exec 4>&-
exec 5>&-


exit 0
```


HTH


----------



## bigb89 (Apr 22, 2014)

Thank you for the response worldi!

I'll be running that script later on and I'll post the results.


----------



## bigb89 (Apr 24, 2014)

Thanks again for the script worldi! It worked. Now I just have to make some tweaks, such as making it compare floats instead of integers and I should be good.


----------



## worldi (Apr 24, 2014)

bigb89 said:
			
		

> making it compare floats



Good luck. Floats and shell scripts don't get along very well. You should probably switch to a language that's suited for the task.

Edit:

After thinking about it for a few minutes I came up with this idea: make the shell script generate an awk() script.   :stud 

Changing the loop section to something like this seems to do the trick:

```
read val1 <&4
read val2 <&5

(
  echo "BEGIN {"
  while true
  do
    echo "if ($val1 > $val2) print $val1; else print $val2;"
    read val1 <&4 || break
    read val2 <&5 || break
  done
  echo "}"
) | awk -f-
```


----------



## bigb89 (May 8, 2014)

I know it's been a while since this thread, but just wanted to give an update...worldi, your awk script worked for the float comparison. BC was another tool that seems to get the job done. With that said, I believe you're correct in regards to using a different scripting language because of the float comparison. Down the road, I may just switch to python and then I might have better solution for that. Thanks again for the help on this.


----------

