我想从两个不同的文本文件中读取(行号相等),并将每一行的第五列相互比较。 但是这对我没有用。 有人帮我吗? 在这里我的代码:
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
在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。