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

Awk 如何编辑Unix文件以在2个特定模式匹配后插入几行,awk,sed,Awk,Sed,我有一个文件,内容如下。我想编辑该文件,以便在文件中看到LTPGY模式时,在“set t table new$m table]”行之后插入一行内容为“FOUND”的新行 输入文件如下所示:- 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

我有一个文件,内容如下。我想编辑该文件,以便在文件中看到LTPGY模式时,在“set t table new$m table]”行之后插入一行内容为“FOUND”的新行

输入文件如下所示:-

  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,未在其他AWK上测试:

gawk '
    /set ra / {found = /LTPGY/}
    1;
    index($0, "set t [ table new $m table]") && found {print "FOUND"}
' file
这可能适用于您(GNU-sed):


将注意力集中在
LTPGY
后面的行上,并找到所需的字符串,将其打印出来,然后用所需的字符串
FOUND

替换除前导空格以外的所有空格。这有点宽泛。您能指出输出文件的确切外观吗?另外,分享你的尝试也很好
sed '/LTPGY/{:a;n;/set t \[ table new $m table\]/!ba;p;s/\S.*/FOUND/}' file