我想从两个不同的文本文件中读取(行号相等),并将每行的第5列相互比较。但它对我不起作用。有人帮我吗?这是我的代码:
df -h --total>current_filesystem_usage.txt
if [ -s previous_months_filesystem_usage.txt ]
then
#reading from two different files and store each line into a variable
while read current_file_line <&3 && read previous_file_line <&4
do
current_filesystem_name=$(cat $current_file_line | awk {'print $1'})
current_filesystem_usage=$(cat $current_file_line | awk {'print $5'})
previous_filesystem_name=$(cat $previous_file_line | awk {'print $1'})
previous_filesystem_usage=$(cat $previous_file_line | awk {'print $5'})
if [ ${current_filesystem_usage%?} -ge ${previous_filesystem_usage%?} ]
then echo "There is problem !!! "
echo "Current disk usage:${current_filesystem_name}, ${current_filesystem_usage}"
echo "Previous month's disk usage:${previous_filesystem_name}, ${previous_filesystem_usage}"
#I also want to store all echo output to output.txt file
elif [ ${current_filesystem_usage%?} -lt ${previous_filesystem_usage%?} ]
then echo "There is no problem. Everything is alright."
echo "Current disk usage: ${current_filesystem_name}, ${current_filesystem_usage}"
echo "Previous month's disk usage: ${previous_filesystem_name}, ${previous_filesystem_usage}"
fi
done 3<current_filesystem_usage.txt 4<previous_months_filesystem_usage.txt
fi
答案 0 :(得分:1)
在awk中:
$ awk '
NR==FNR { # process the first file
a[FNR]=$5 # hash field $5 to a, use row number as key
next } # move to next record
$5 > a[FNR] { # process the second file, if current is greater than previous
print "error" # output error
}
' file1 file2
它基本上将file1哈希到a
并使用行号作为键。你没有在你的帖子中提到比较结果是什么,所以无法帮助更多的ATM。