2个文件内容的比较
文件内容比较shell 如:
a.txt
2111,12,222
b.txt
2121,12,222
shell代码
#!/bin/bash
touch result.txt;
echo '创建结果文件成功!';
echo '' > result.txt;
FILENAME1='/home/zzq/a.txt';
#拿到文件的行数
FILECOUNT1=`awk 'END{print NR}' $FILENAME1`
#echo $FILECOUNT1;
FILENAME2='/home/zzq/b.txt'
FILECOUNT2=`awk 'END{print NR}' $FILENAME2`
#遍历行数
for((i=1;i<=FILECOUNT1;i++))
do
#echo $i
#用sed命令读取每行
readLine=`sed -n "${i}p" $FILENAME1`
readLine2=`sed -n "${i}p" $FILENAME2`
#echo $readLine > cache1.txt;
#echo $readLine2 > cache2.txt;
#echo $readLine
#将行用,逗号分割为数组
OLD_IFS="$IFS"
IFS=","
arr=($readLine)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
echo "$s"
done
#echo -----+ ${arr[2]};
OLD_IFS="$IFS"
IFS=","
arr2=($readLine2)
IFS="$OLD_IFS"
for s in ${arr2[@]}
do
echo "$s"
done
#echo -----+ ${arr2[1]}
lengthVal=${#arr[@]}
echo 数组长度 $lengthVal;
#遍历数组比较字符串
for ((num=0;num<lengthVal;num++))
do
# echo ${arr[${num}]}
if [ "${arr[${num}]}" != "${arr2[${num}]}" ];then
resultText="${arr[${num}]} 不相等在$FILENAME1的第$i行第$(($num+1))列"
echo $resultText;#输出结果
echo $resultText >> result.txt;
fi
done
#sleep 1s
done