该文件的内容如下
101,abc,2017-03-15,Linux
222,xyz,2016-10-10,Unix
987,def,2017-01-03,Solaris
567,tek,2014-01-09,windows
Trailer|20170331|4
我需要比较4的实际记录行,等于预告片(4)上的总记录。
答案 0 :(得分:0)
我做了几个假设:
您没有指定您正在使用的特定操作系统或shell,因此您可能需要调整一些命令,因为它们之间存在细微差别。
无论如何,这就是我提出的:
recchk()
{
#Here's the file we're working with:
myfile=testfile.txt
#Let's figure out how many records are in it
recs=`wc -l $myfile | cut -f1 -d" "`
#Now subtract 1 because we don't want to count the trailer
let recs=`expr $recs - 1`
echo "There are $recs data records in the file."
#Now we'll find the trailer record and get the third field
trcnt=`tail -1 $myfile | cut -f3 -d"|"`
echo "Trailer says there are $trcnt records."
#Now we'll check to make sure all is well.
if [ $recs -ne $trcnt ]; then
echo "Uh-oh! They don't match\!"
else
echo "Right on, daddy-o\! We're righteous\!"
fi
}
希望这有帮助!