我想从两个不同的文本文件中读取(行号相等),并将每行的第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
最佳答案
在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。关于linux - 从两个不同的文本文件中读取并获取每一行的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44009855/