【问题标题】:AWK to search a specific sequence and if found search in the next line another sequence version2AWK 搜索特定序列,如果找到则在下一行搜索另一个序列版本2
【发布时间】:2021-05-20 15:44:09
【问题描述】:

我正在尝试查找一个 txt 格式的字符串,每次找到它时,然后寻找一个特定的字符串来更改另一个字符串,并避免读取该行的第一个序列。

想象下一个十六进制 txt:

0000  09 06 07 04 00 00 01 00 1d 03 78 2c a1 2a 02 01   
0010  b7 09 01 47 30 22 a0 0a 4b 08 33 04 03 92 22 14   
0020  17 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83   
0030  07 91 94 71 06 00 07 19

0000  09 06 07 04 00 00 01 00 2b 03 4b 27 a1 25 02 01   
0010  00 09 01 66 30 4b a0 0a 80 08 33 04 03 92 22 14   
0020  17 f0 a1 06 82 00 84 00 85 00 82 07 91 94 71 06   
0030  00 07 19

预期输出:

0000  09 06 07 04 00 00 01 00 1d 03 78 2c a1 2a 02 01   
0010  b7 09 01 47 30 22 a0 0a 4b 08 33 04 03 92 22 25   
0020  17 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83   
0030  07 91 94 71 06 00 07 19

0000  09 06 07 04 00 00 01 00 2b 03 4b 27 a1 25 02 01   
0010  00 09 01 66 30 4b a0 0a 80 08 33 04 03 92 22 25   
0020  17 f0 a1 06 82 00 84 00 85 00 82 07 91 94 71 06   
0030  00 07 19

我需要每次遇到 4b 序列来查找 14 序列,如果找到则在下一行查找第一个字符串,在本例中为 17,如果此字符串为 17,则将 14 更改为 25。你在左边的是一个序列,它给出了你所在的 txt 行,所以分析起来并不有趣,因为它在每个段落中都重复了

我拥有的是下一个:

gawk  ' { for ( i = 1; i <= NF; ++i ) {

    if ( $i == "4b" )
        r = 1
    if ( r && ($i == "14" ))
        t = 1

  }
}
1 ' example.txt example2.txt

我不知道如何跳转开始阅读下一行

总结:

这个想法是搜索4b,如果遇到,然后开始在同一行中寻找14,它必须在该行的最后一个位置。 如果在下一行的第二个字段中出现 17 然后将 14 更改为 25

我拥有的是这样的:

gawk -i inplace  ' { for ( i = 1; i <= NF; ++i ) {

    if ($i == "4b" )
        r = 1
    if (r && $i == "14" )
        z = 1
       $i = x
    if ( z && r && $i == "17")
        z = 0
        r = 0
        x = "25"


  }
}
1 ' example.txt example2.txt

主要问题是我不知道如何验证14是否是下一行的第二个字段

【问题讨论】:

  • 14 是否总是在4b 之后的下一行? 14 是否总是第二行的最后一个字段?
  • 第一个问题,不要总是这样。关于第二个问题,是的。我只对研究14 是最后一个字段的情况感兴趣。在这种情况下,我认为该行中有 17 字段。通过这种方式进行操作将是一个不错的选择。正在研究是否出现4b。如果1417 字段中并且17 在下一行的第二个字段中,则将14 更改为25 @anubhava

标签: awk


【解决方案1】:

使用gnu-awk,你可以试试这个:

awk -v RS= '{ORS=RT} {$0 = gensub(/(\s4b\s(.+\s)?)14([[:blank:]]*\n\S+[[:blank:]]+17\s)/, "\\125\\3", 1)} 1' file

0000  09 06 07 04 00 00 01 00 1d 03 4b 2c a1 2a 02 01
0010  b7 09 01 47 30 22 a0 0a 80 08 33 04 03 92 22 25
0020  17 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83
0030  07 91 94 71 06 00 07 19

0000  09 06 07 04 00 00 01 00 2b 03 4b 27 a1 25 02 01
0010  00 09 01 66 30 1d a0 0a 80 08 33 04 03 92 22 25
0020  17 f0 a1 06 82 00 84 00 85 00 82 07 91 94 71 06
0030  00 07 19

RegEx Demo

【讨论】:

  • 你能解释一下吗?
  • 是的,您也可以查看链接的 deno 以获得解释。请检查您的问题下方的问题并提供说明
  • 这不正确。请检查链接的演示和我生成的响应。两者都在第二行的最后一列显示25 两次。
【解决方案2】:

仅使用您展示的示例,您能否尝试以下操作。使用 GNU awk 编写和测试。

awk '
!NF{ found1=found2=prevLine=prevRestLine=0 }
/(^|[[:space:]])4b([[:space:]]|$)/{
  found1=1
  print
  next
}
found1 && /(^|[[:space:]])14([[:space:]]|$)/{
  found2=1
  prevLine=$0
  match($0,/[[:space:]]+$/)
  s=substr($0,RSTART,RLENGTH)
  sub(/[0-9]+[[:space:]]+$/,"")
  prevRestLine=$0
  next
}
found1 && found2{
  if($2==17 && prevLine && prevRestLine){
    print prevRestLine 25 s ORS $0
    prevLine=prevRestLine=0
  }
  if($2!=17 && prevLine){
    print prevLine ORS $0
    prevLine=0
  }
  found1=found2=0
  next
}
1
' Input_file

说明:为上述添加详细说明。

awk '                                            ##Starting awk program from here.
!NF{ found1=found2=prevLine=prevRestLine=0 }     ##If line is null then reset.
/(^|[[:space:]])4b([[:space:]]|$)/{              ##if 4b is present(with spaces or without) then:
  found1=1                                       ##Set found1
  print                                          ##print current line.
  next                                           ##Leave all other statements from here.
}
found1 && /(^|[[:space:]])14([[:space:]]|$)/{    ##Checking if found1 is set AND 14 is found(with or without space)
  found2=1                                       ##Set found2 here.
  prevLine=$0                                    ##Set prevLine value to current line.
  match($0,/[[:space:]]+$/)                      ##Get ending spaces of line.
  s=substr($0,RSTART,RLENGTH)                    ##create s with above matched values.
  sub(/[0-9]+[[:space:]]+$/,"")                  ##Substitute digits spaces at last with NULL in current line.
  prevRestLine=$0                                ##Set prevRestLine value to current line.
  next                                           ##Leave all other statements from here.
}
found1 && found2{                                ##Checking if found1 and found2 are set.
  if($2==17 && prevLine && prevRestLine){        ##Checking if 2nd field is 17 and prevLine, prevRestLine are set.
    print prevRestLine 25 s ORS $0               ##Printing prevRestLine 25 s ORS $0 here.
    prevLine=prevRestLine=0                      ##unset here.
  }
  if($2!=17 && prevLine){                        ##If 2nd column is not 17 AND prevLine is set.
    print prevLine ORS $0                        ##Printing prevLine ORS and current line.
    prevLine=0                                   ##unset prevLine here.
  }
  found1=found2=0                                ##unset found1 and found2 here.
  next                                           ##Leave all other statements from here.
}
1                                                ##1 will print current line.
' Input_file                                     ##Mentioning Input_file name here.

【讨论】:

    猜你喜欢
    • 2014-09-13
    • 2016-07-15
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多