【问题标题】:Processing a text file with awk, sed and grep使用 awk、sed 和 grep 处理文本文件
【发布时间】:2014-01-19 17:23:15
【问题描述】:

我的输入文件:

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*$/ /'

非常感谢您。

【问题讨论】:

  • 你能告诉我们你想要的输出吗?

标签: sed awk grep


【解决方案1】:

这里是从 #1 到 #3:

awk '{$2="";sub(/  /," ")} !/9999.[0-9]/ && t++>2' $FILE

不确定您喜欢在第 4 步中计算什么,您能否说得更清楚一些。

【讨论】:

  • 在第 4 步,我想计算最后(最后两个字符)值为 '00' 的行之间的行(在第一列中);如果结果 >=3:只保留末尾有 '00' 的行,并删除其他行;如果结果是
【解决方案2】:

我将 #1​​-3 基于 Jotne 的工作并添加了一个函数来处理 #4。以下内容应该放入一个可执行文件(我称之为awko)并像awko Constanta20110513.txt一样运行:

#!/usr/bin/awk -f

BEGIN { print "YY/MM/DD HH -Level- Atm.Prs -Tw-" }

# absorb jotne's work for #1-3 more or less
{$2="";sub(/  /," ")}
/9999.0000/ || NR<=3 { next }

/^[0-9]{12}00/ { output_line() } # deal with the "00" lines

END { output_line() } # output the final "00" stored in last

function output_line() {
    if( last_nr != 0 ) {
        if( NR-last_nr < 3 ) {
            temp = $0          # save off the current line
            $0 = last          # reset it to the last "00" line
            $2 = "9999.0000"   # make $2 what you want
            print $0
            $0 = temp          # restore $0 from temp
        }
        if( NR-last_nr >= 3 ) { print last }
    }
    $1 = substr( $1, 1, 12 )   # drop the "00" from $1
    last = $0; last_nr = NR;   # store some variables
    }

我从您指定的输入中得到以下输出:

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 1013.50 13.2 0 0.0 0
201105120801 1.0065 1013.48 13.2 0 0.0 0
201105120802 9999.0000 1013.51 13.2 0 0.0 0

【讨论】:

  • @n0741337工作完美!非常非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2012-07-11
  • 2021-07-19
  • 2022-11-30
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
相关资源
最近更新 更多