2017-05-16 7 views
2

2つの異なるテキストファイル(行番号が等しい)を読み込み、各行の5番目の列を互いに比較したいと考えています。しかし、それは私にとってはうまくいかなかった。誰か助けてくれますか?ここに私のコード:AWKで2つの異なるテキストファイルから読み込んで各行の列を取得する

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

差分を比較して計算したい場合は、 '-h'の代わりに' -m'を使用することができます。人間が読める形式ではなく、よく解析された形式ではなく、結果を整数値としてメガバイト単位で出力します。 –

+0

私は用途のパーセンテージを比較したいと思います。メガバイトではない。 – Serhat

答えて

1

$ 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 

それは基本的にaにFILE1をハッシュし、キーとして行番号を使用しています。比較の結果が何であるかをあなたの郵便では言及していないので、ATMをもっと助けることはできません。

+0

少し説明していただけますか?たとえば、何ですか、なぜ$ 0で始まるのですか? – Serhat

+1

比較の結果、現在のエコーエラーが以前のエコーエラーよりも大きければ、すべて正常です。 – Serhat

+1

'a'はハッシュで、' file1'が処理されるときに '$ 5'が格納され、キーとして行番号が格納されます。 –

関連する問題