I have the following testfile.txt:
string1 1
string2 2
string3 3
string1 2
string2 4
string3 6
string1 3
string2 6
string3 9
I have the following script:
#!bin/sh
oldstr="abc"
while read line
do
newstr=\`echo "$line" | sed 's/\(.*\)\( \)\([0-9].*\)/\1/'\`
echo "$oldstr vs $newstr"
if [ $oldstr == $newstr ]
then
echo "old $oldstr"
else
oldstr=$newstr
echo "new $oldstr"
fi
done < $@ | sort
echo "done"
I then run the script:
$ sh test.sh testfile.txt
and get the output:
abc vs string1
new string1
new string1
new string1
new string2
new string2
new string2
new string3
new string3
new string3
string1 vs string2
string1 vs string2
string1 vs string2
string2 vs string3
string2 vs string3
string2 vs string3
string3 vs string1
string3 vs string1
done
I'd like to know why, as I would expect the output to be:
abc vs string1
new string1
string1 vs string1
old string1
string1 vs string1
old string1
string1 vs string2
new string2
and so on.
- Why isn't the "if" statement working?
- Why is the echo printing out of sequence?