AWK / grep的替代第二节比赛的第一场比赛后的文本文件,而在foreach循环 [英] awk/grep replace 2nd match after first match in text file while in foreach loop

查看:85
本文介绍了AWK / grep的替代第二节比赛的第一场比赛后的文本文件,而在foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我会尽力来形容这里我的问题。我有一个看起来像这样的文件:

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

WP00站34.07335 -106.91932 1.43

sta WP00 34.07335 -106.91932 1.43

时间2013年10月23日二十时10分17秒。

time 10/23/2013 20:10:17

净XO

数据记录仪Passcal_q330_linear 0100000EAA23E50F#2847

datalogger Passcal_q330_linear 0100000EAA23E50F # 2847

传感器trillium_240_2 0 583

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

添加

接近传感器trillium_240_2 2013年10月23日20点10分17秒。

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

传感器trillium_120 0 279种

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

添加

接近传感器trillium_120 2013年10月23日20时10分35秒。

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

WP00站34.07335 -106.91932 1.43

sta WP00 34.07335 -106.91932 1.43

时间2013年10月28日20点20分28秒。

time 10/28/2013 20:20:28

净XO
数据采集​​器Passcal_q330_linear 0100000EAA23E50F#2847

net XO datalogger Passcal_q330_linear 0100000EAA23E50F # 2847

传感器trillium_240_2 0 583

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

添加

接近传感器trillium_240_2 2013年10月28日20时二十分28秒

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

传感器trillium_120 0 268

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

添加

接近传感器trillium_120 2013年10月28日二十时20分45秒。

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

对不起,给你的文件这么长的一部分,但我要确保你看到的一切。有元数据的每个站的多块(站,在这种情况下是WP00)。我想,以取代第一和第二关闭的语句时,

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,

即,接近传感器trillium_240_2 2013年10月23日20时10分17秒和
接近传感器trillium_120 2013年10月23日20点10分35秒
与第三紧密声明的日期和时间。所以,我希望他们能够阅读关闭传感器trillium_240_2 2013年10月28日20时28分28秒和接近传感器trillium_120 2013年10月28日20时28分28秒,等等等等的站项的每个块。

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.

要放在不同的方式,我想用第二匹配密切陈述时间和日期之前它来取代第2位。或者你可以把它看作第三匹配紧密声明日期和时间更换的precede它的前两个匹配的时间和日期。

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.

此外,使这个更难,如果它的最后一站进入我想让关闭时间和日期是2500年12月31日23:59:59。

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.

我有我所站的循环在这个文本文件,所以我希望能够为条目的第n任何金额自动做到这一点。我使用grep,awk的尝试,并以各种方式来获得这种逻辑工作的sed,但似乎无法推测出来。在正确的方向任何帮助或推将是巨大的。在此先感谢!

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!

推荐答案

这个命令应该做你想要什么:

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

这篇关于AWK / grep的替代第二节比赛的第一场比赛后的文本文件,而在foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆