如何编辑UNIX文件以在2个特定模式匹配后插入几行



我有一个文件,其中包含如下内容。我想编辑文件,以便每当在文件中看到LTPGY模式,然后在"设置T表新$ M表"之后,插入一个新的线,其中包含"找到"的内容。

输入文件如下: -

  set ra [ 1 22 3 ] LTPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] STPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] LTPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] STPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

我希望输出像: -

  set ra [ 1 22 3 ] LTPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  FOUND
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] STPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] LTPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  FOUND
  set a [  $t process_axis]
  set a [  $t voltage_axis]

  set ra [ 1 22 3 ] STPGY
  set a 0
  set a1 1
  set t [ table new $m table]
  set a [  $t process_axis]
  set a [  $t voltage_axis]

与Gawk一起工作,未经测试的其他尴尬:

gawk '
    /set ra / {found = /LTPGY/}
    1;
    index($0, "set t [ table new $m table]") && found {print "FOUND"}
' file

这可能对您有用(gnu sed):

sed '/LTPGY/{:a;n;/set t [ table new $m table]/!ba;p;s/S.*/FOUND/}' file

将重点放在LTPGY之后的线上并查找所需的字符串,然后用所需的字符串FOUND替换所有领先空间。

最新更新