【问题标题】:How to grep similiar values one by one under each other?如何在彼此下一个一个地grep相似的值?
【发布时间】:2020-11-30 16:44:25
【问题描述】:

我有来自文件输出的 grep,例如:

2531 POKRZYWNIAK KRZYSZTOF 244 18/01 2 13:46 23:26
3346 SOROTA DARIUSZ 244 18/01 1 04:05 13:46

需要让它看起来像这样:

2531 POKRZYWNIAK KRZYSZTOF 244 18/01 2 13:46 23:26
3346 SOROTA DARIUSZ_________244 18/01 1 04:05 13:46

没有“”字符 - 表示将其隔开,直到相似/相同的值彼此相加。 4 位数字下的 4 位数字,名称下的名称,3 位名称(例如 244)多字符,如 18/01 下的 18/01 字符串,小时下的小时,分​​钟下的分钟。输出应如下所示,不包括我用来演示所需空间的“”字符。

我正在使用cat filename.txt | grep -w $criteria

我还使用 grep 和 -i 来区分大小写。

【问题讨论】:

  • 您能否确认一下您的文件中是否总是有 2 行?
  • 为什么DARIUSZKRZYSZTOF 不一致?

标签: linux bash awk grep cat


【解决方案1】:

如果输出是制表符分隔的,请像这样使用column

cat filename.txt | grep PATTERN | column -t -s $'\t'

您也可以使用不带catgrep

grep PATTERN filename.txt | column -t -s $'\t'

【讨论】:

    【解决方案2】:

    您能否根据您显示的示例尝试以下操作,这些示例仅在 GNU awk 中编写和测试。根据 OP 的描述,使用正则表达式在行中匹配 3 digits space 2 digits/2 digits 模式。

    awk '
    BEGIN{
      OFS="\t"
    }
    match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){
      firstPart=substr($0,1,RSTART-1)
      sub(/[^ ]* +/,"",firstPart)
      restPart=substr($0,RSTART,RLENGTH)
      sub(/ +/,OFS,restPart)
      print $1,firstPart,restPart
    }
    ' Input_file | column -t -s $'\t'
    

    输出如下。

    2531  POKRZYWNIAK KRZYSZTOF   244  18/01 2 13:46 23:26
    3346  SOROTA DARIUSZ          244  18/01 1 04:05 13:46
    

    说明:为上述解决方案添加详细说明。

    awk '                                        ##Starting awk program from here.
    BEGIN{                                       ##Starting BEGIN section of this program from here.
      OFS="\t"                                   ##Setting output field separator as TAB here.  
    }
    match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){   ##Using match function to match 3 digits space 2 digits/2 digits.
      firstPart=substr($0,1,RSTART-1)            ##Creating firstPart which has sub string from 1st position to till RSTART-1
      sub(/[^ ]* +/,"",firstPart)                ##Substituting till space everything with NULL in firstPart here.
      restPart=substr($0,RSTART,RLENGTH)         ##Creating restPart with substring of matched regex in match function.
      gsub(/ +/,OFS,restPart)                    ##Globally Substituting spaces with TAB in restPart.
      print $1,firstPart,restPart                ##Printing first field, firstPart and restPart here.
    }
    ' Input_file | column -t -s $'\t'            ##Mentioning Input_file and sending awk output to column command to get good output.
    

    【讨论】:

      【解决方案3】:
      $ awk -v OFS='\t' '{
          name = $0
          gsub(/[[:space:]]+/," ",name)
          gsub(/^[^ ]+ |( [^ ]+){5}$/,"",name)
          print $1, name, $(NF-4), $(NF-3), $(NF-2), $(NF-1), $NF
      }' file | column -s$'\t' -t
      2531  POKRZYWNIAK KRZYSZTOF  244  18/01  2  13:46  23:26
      3346  SOROTA DARIUSZ         244  18/01  1  04:05  13:46
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-30
        • 2021-04-14
        • 1970-01-01
        • 2019-08-02
        • 2018-10-22
        • 2018-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多