Reputation: 3105
I have two files file1
104.128.225.208:8000
103.27.24.114:80
104.128.225.208:8000
and file2
103.27.24.114:99999999
103.27.24.114:88888888888
104.128.225.208:8000
103.27.24.114:80
104.128.225.208:8000
and in file2 there are two new lines
103.27.24.114:99999999
103.27.24.114:88888888888
So I want to check if there are new lines in file
for i in $(cat $2)
do
for j in $(cat $1)
do
if [ $i = $j ]; then
echo $i
fi
done
done
/.program file1 file2
but I don't get expected output. I think that my if statement is not working fine. What I'm doing wrong?
Upvotes: 0
Views: 233
Reputation: 140748
Your problem is probably that you are looping over every line in file1 for each line in file2.
The comm
utility does what you want, but it assumes both files are sorted.
$ sort file1 -o file1
$ sort file2 -o file2
$ comm -13 file1 file2
103.27.24.114:99999999
103.27.24.114:88888888888
Upvotes: 3
Reputation: 84579
This is what diff
is for. Example:
$ diff dat/newdat1.txt dat/newdat2.txt
0a1,2
> 103.27.24.114:99999999
> 103.27.24.114:88888888888
Where newdat1.txt
and newdat2.txt
are:
104.128.225.208:8000
103.27.24.114:80
104.128.225.208:8000
and
103.27.24.114:99999999
103.27.24.114:88888888888
104.128.225.208:8000
103.27.24.114:80
104.128.225.208:8000
You can simply test the return of diff
with or without output depending on the options and your needs. (e.g. if diff -q $file1 $file2 >/dev/null; then echo same; else echo differ; fi
)
Upvotes: 1
Reputation: 12625
#!/bin/bash
for n in $(diff file1 file2); do
if [ -z "$firstLineDiscarded" ]; then
firstLineDiscarded=TRUE
elif [ $n != ">" ]; then
echo $n
fi
done
If you're not attached to that particular approach this seems to work.
Of course it breaks down if the input syntax changes (includes spaces in the data), but for this strict application... maybe good enough.
Upvotes: 0