待處理的檔案aaa.log內容:
09/26 00:00:08 116.41834259033203 39.90071597768346 116.60888671875 35.38904996691167 502516.707409
09/26 00:01:34 121.13089397549632 30.98941357306606 121.55966117978099 31.149880293437714 44614.8976635
09/26 00:01:51 117.12383329868318 39.243494180634734 117.12383329868318 39.243494180634734 0
09/26 00:03:55 117.12383329868318 39.243494180634734 117.12383329868318 39.243494180634734 0
09/26 00:04:07 106.70314550399783 26.556866721950268 106.72096878290178 26.58711559252519 3806.24567829
09/26 00:05:23 117.12383329868318 39.243494180634734 117.12383329868318 39.243494180634734 0
09/26 00:05:38 114.18694585561754 36.686433818538205 115.66599234938623 37.7350984521996 175556.820747
09/26 00:05:57 121.13089397549632 30.98941357306606 121.55966117978099 31.149880293437714 44614.8976635
處理要求:
最後一列的數值小於50000的行,且之抽取第三列至第六列的內容,儲存到另一個檔案BBB.txt
shell指令碼程式:
#!/bin/bash
echo ' ' > BBB.txt
while read LINE #逐行讀取檔案“/bak/log/aaalog”
do
distance=$(echo "$LINE"| awk -F '\t' '{print $7}') #將每行的第七列取出賦值給distance, 賦值文法: temp=$(賦值內容) 或者 temp=`賦值內容`, 注意不是單引號或雙引號。
#if [[ $distance < 3000 ]]; then
# echo "$LINE"| awk -F '\t' '{print $3,$4,$5,$6}' >> BBB.txt
#fi
[ $distance < 50000 ] && echo "$LINE"| awk -F '\t' '{print $3,$4,$5,$6}' >> BBB.txt #取出第七列小於50000的行且將指定列輸出到檔案BBB.txt
done < /bak/log/aaalog
#注意:
#條件比較文法: if[比較條件]]; then...要做的事情 fi
#或者:[比較條件] && 要做的事情。