awk/grep replace 2nd match after first match in text file while in foreach loop

Deadly 提交于 2020-01-06 15:21:50

问题


Okay I will try to describe my problem here. I have a file that looks like this:

sta WP00 34.07335 -106.91932 1.43

time 10/23/2013 20:10:17

net XO

datalogger Passcal_q330_linear 0100000EAA23E50F # 2847

sensor trillium_240_2 0 583

    axis Z 0 0 - 1 1
    axis N 0 90 - 2 1
    axis E 90 90 - 3 1
    samplerate 40sps
    channel Z BHZ 00
    channel N BHN 00
    channel E BHE 00
    samplerate 1sps
    channel Z LHZ 00
    channel N LHN 00
    channel E LHE 00

add

close sensor trillium_240_2 10/23/2013 20:10:17

sensor trillium_120 0 279

    axis Z 0 0 - 4 1
    axis N 0 90 - 5 1
    axis E 90 90 - 6 1
    samplerate 40sps
    channel Z BHZ 01
    channel N BHN 01
    channel E BHE 01
    samplerate 1sps
    channel Z LHZ 01
    channel N LHN 01
    channel E LHE 01

add

close sensor trillium_120 10/23/2013 20:10:35

#

sta WP00 34.07335 -106.91932 1.43

time 10/28/2013 20:20:28

net XO datalogger Passcal_q330_linear 0100000EAA23E50F # 2847

sensor trillium_240_2 0 583

    axis Z 0 0 - 1 1
    axis N 0 90 - 2 1
    axis E 90 90 - 3 1
    samplerate 40sps
    channel Z BHZ 00
    channel N BHN 00
    channel E BHE 00
    samplerate 1sps
    channel Z LHZ 00
    channel N LHN 00
    channel E LHE 00

add

close sensor trillium_240_2 10/28/2013 20:20:28

sensor trillium_120 0 268

    axis Z 0 0 - 4 1
    axis N 0 90 - 5 1
    axis E 90 90 - 6 1
    samplerate 40sps
    channel Z BHZ 01
    channel N BHN 01
    channel E BHE 01
    samplerate 1sps
    channel Z LHZ 01
    channel N LHN 01
    channel E LHE 01

add

close sensor trillium_120 10/28/2013 20:20:45

Sorry to give you such a long part of the file but I want to make sure you see everything. There are more blocks of meta-data for each station (sta, in this case is WP00). I want to replace the time of the first and second "close" statements,

i.e., close sensor trillium_240_2 10/23/2013 20:10:17 and close sensor trillium_120 10/23/2013 20:10:35 with the third close statement date and time. So I want them to read close sensor trillium_240_2 10/28/2013 20:28:28 and close sensor trillium_120 10/28/2013 20:28:28, and so on and so forth for each block of station entries.

To put it a different way I want to use the 2nd matched close statement time and date to replace the first two before it. Or you could think of it as the 3rd matched close statement time and date replacing the first two matched time and dates that precede it.

Additionally to make this harder, if it's the last station entry I want to make the close time and date be 12/31/2500 23:59:59.

I have this text file that I am looping over by station, so I would like to be able to do this automatically for any nth amount of entries. I've tried using grep, awk, and sed in various way to get this logic to work but can't seem to figure it out. Any help or push in the right direction would be great. Thanks in advance!


回答1:


this command should do what you want:

awk 'NR==FNR{
        if(/^close/){i++
            if(i<3)a[NR]=$1FS$2FS$3
            if(i==3)
                for(x in a)
                    a[x]=a[x]FS$4FS$5
            l=NR
        }
        next
    } 
    FNR in a{$0=a[FNR]}
    FNR==l{$0=$1FS$2FS$3FS"12/31/2500 23:59:59"}7' file file


来源:https://stackoverflow.com/questions/20107224/awk-grep-replace-2nd-match-after-first-match-in-text-file-while-in-foreach-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!