我的输入文件:处理与AWK一个文本文件,sed和grep的
20110512075615 Constanta 1.0041 1013.41 9999.0 0 0.0 0
20110512075630 Constanta 1.0021 1013.45 9999.0 0 0.0 0
20110512075645 Constanta 1.0031 1013.47 9999.0 0 0.0 0
20110512075700 Constanta 1.0018 1013.47 9999.0 0 0.0 0
20110512075730 Constanta 1.0038 1013.48 9999.0 0 0.0 0
20110512075745 Constanta 1.0023 1013.48 9999.0 0 0.0 0
20110512075800 Constanta 9999.0000 1013.46 13.2 0 0.0 0
20110512075815 Constanta 1.0038 1013.45 13.2 0 0.0 0
20110512075830 Constanta 1.0040 1013.50 13.2 0 0.0 0
20110512075845 Constanta 1.0034 1013.50 13.2 0 0.0 0
20110512075900 Constanta 1.0050 1013.45 13.2 0 0.0 0
20110512075915 Constanta 1.0060 1013.48 13.2 0 0.0 0
20110512075930 Constanta 1.0056 1013.45 13.2 0 0.0 0
20110512080000 Constanta 1.0066 1013.50 13.2 0 0.0 0
20110512080015 Constanta 1.0067 1013.49 13.2 0 0.0 0
20110512080100 Constanta 1.0065 1013.48 13.2 0 0.0 0
20110512080115 Constanta 9999.0000 1013.51 13.2 0 0.0 0
20110512080130 Constanta 1.0065 1013.51 13.2 0 0.0 0
20110512080145 Constanta 1.0079 1013.49 13.2 0 0.0 0
20110512080200 Constanta 1.0072 1013.51 13.2 0 0.0 0
20110512080215 Constanta 1.0084 1013.51 13.2 0 0.0 0
我的输出文件:
YY/MM/DD HH -Level- Atm.Prs -Tw-
201105120757 1.0018 1013.47 9999.0 0 0.0 0
201105120759 1.0050 1013.45 13.2 0 0.0 0
201105120800 9999.0000 1.0066 1013.50 13.2 0 0.0 0
201105120801 1.0065 1013.48 13.2 0 0.0 0
201105120802 9999.0000 1.0072 1013.51 13.2 0 0.0 0
我的代码:
#! /bin/bash
FILE="Constanta20110513.txt"
# 1) remove column two(='Constanta')
awk '{$2="";print}' $FILE | column -t > tmpfile
# 2) remove lines with '9999.0000'
cat tmpfile | sed -e '/9999.[0-9]/d' >> final.tmp
# 3) remove first three lines
awk 'NR>3' final.tmp >> myfile.tmp
# 4) count lines between '....00' si '....00':
#if >= 3, keep only the line with '...00' and delete the other lines
#if < 3, do the same, and put '9999' on column two
output=$(grep -n '00\s*$' myfile.tmp | sed 's/\s*$/ /')
array=($output $(cat myfile.tmp | wc -l))
for ((i=0; i<${#array[@]}-1; i++)); do
index1=$(echo "${array[$i]}" | grep -o '^[0-9]*')
index2=$(echo "${array[$i+1]}" | grep -o '^[0-9]*')
if [ $((index2 - index1)) -ge 3 ]; then
echo $(echo "${array[$i]}" | grep -o '[0-9]*$') >> temp.tmp
else
echo $(echo "${array[$i]}" | grep -o '[0-9]*$') 9999.0000 >> temp.tmp
fi
done
# 5) delete last two characters from first column(=00)
awk '{sub(/..$/,"",$1)} 1' temp.tmp >> output.tmp
# 6) insert header
echo 'YY/MM/DD HH -Level- Atm.Prs -Tw-' | cat - output.tmp >> output2.tmp
#save
mv output2.tmp $FILE
我的问题是,在步骤4:不工作,临时文件temp.tmp不是创建的。 我认为问题在这里:grep -n '00\s*$' myfile.tmp | sed 's/\s*$/ /'
。
非常感谢您提前。
你能否告诉我们输出你的愿望? – Beta