簡體   English   中英

提取方括號之間的兩個文本之間的字符串

[英]Extract strings between 2 texts enclosed between squared brackets

我有類似於下面的字符串

1. the quick brown `[fox].[jumps]` [over] the lazy dog
 2. the quick brown fox [jumps] [over] the lazy dog
 3. `[the].[quick]` brown `[fox].[jumps]` [over] the lazy dog

我需要提取以下值

 1. fox.jumps
 2. <Nothing>
 3. the.quick, fox.jumps

請你幫我處理 shell 腳本中的正則表達式?

對於多字符 RS、RT 和 gensub(),使用 GNU awk:

$ awk -v RS='[[][^]]*][.][[][^]]*]' 'RT{print gensub(/[][]/,"","g",RT)}' file
fox.jumps
the.quick
fox.jumps

使用您顯示的示例,您能否嘗試以下操作。 在 GNU awk中編寫和測試。

awk '
{
  val=""
  for(i=1;i<=NF;i++){
    if($i~/^\[.*]\.\[.*]$/){
      gsub(/[][]/,"",$i)
      val=(val?val ", ":"")$i
    }
  }
  print (val==""?"<Nothing>":val)
}'  Input_file

樣品 output 將按照所示樣品如下。

fox.jumps
<Nothing>
the.quick, fox.jumps

這是另一個單行...從 shell 的角度來看(您可以刪除\newline使其成為一行)。

確保\始終是該行的最后一個字符,之后沒有空格。

gawk '{\
  for(i=1;i<NF;i++)\
  {\
     if(match($i,/\]\.\[/)>0)\
     {\
         for(k=1;k<length($i);k++)\
         {\
            c=substr($i,k,1);\
            if(c!="[" && c!="]")\
            printf("%s",c);\
         }\
         printf(" ");\
     }\
  }\
  printf("\n");\
}' example.txt

Anyway, it would be useful to put the gawk-code in between ' and ' into a file (file.awk, in file.awk remove all \ ) and then call, gawk like so, meaning test.awk starts with { and ends與} 這可能不是一個優雅的解決方案,但您可以在其中添加更多內容,例如許多變量、整個程序、子例程......

gawk -f test.awk example.txt
Output:
fox.jumps 

the.quick fox.jumps

使用sed (在s/命令中支持\n )。

sed '
    s/$/\n/
    : again
    /\([^\n]*\)\[\([^]]*\)\]\.\[\([^]]*\)\]\([^\n]*\)\n/{
        s//\1\4\n\2.\3\n/
        b again
    }
    s/[^\n]*\n//
    s/\n$//
    s/\n/, /g
'
  • s/$/\n/在讀取行的末尾添加一個換行符。 在沒有任何正則表達式的行的情況下很重要。
  • : again again label ,你可以 go 到
  • /../ - 匹配一個正則表達式
    • \([^\n]*\)匹配任何非換行符並記住它在\1
    • \[\([^]]*\)\]\.\[\([^]]*\)\]匹配[somethign].[something]並記住\2\3中的部分
    • \([^\n]*\) - 匹配任何非換行符
  • /.../{ - 當正則表達式匹配時 - s// - 重用最后一個正則表達式,即。 上面的一個 - /\1\4\n\2.\3\n/ - 隨機輸入,以便將不感興趣的部分放在換行符之前,並在換行符之后提取有趣的部分 - b again - go 再次到匹配另一個模式
  • s/[^\n]*\n//刪除行中不匹配的部分
  • s/\n$// - 刪除尾隨換行符
  • s/\n/, /g用逗號和空格分隔部分。

例子:

$ sed 's/$/\n/; : again; /\([^\n]*\)\[\([^]]*\)\]\.\[\([^]]*\)\]\([^\n]*\)\n/{ s//\1\4\n\2.\3\n/; b again; }; s/[^\n]*\n//; s/\n$//; s/\n/, /g' <<EOF
the quick brown [fox].[jumps] [over] the lazy dog
the quick brown fox [jumps] [over] the lazy dog
[the].[quick] brown [fox].[jumps] [over] the lazy dog
EOF

輸出:

fox.jumps

the.quick, fox.jumps

如果您不希望中間有空行,那么在沒有找到模式的情況下,不要 output 中的任何 sed 中的任何內容。 添加sed -n並在腳本末尾不要 output 如果為空 - /^$/!p ,像這樣:

sed -n 's/$/\n/; : again; /\([^\n]*\)\[\([^]]*\)\]\.\[\([^]]*\)\]\([^\n]*\)\n/{ s//\1\4\n\2.\3\n/; b again; }; s/[^\n]*\n//; s/\n$//; s/\n/, /g; /^$/!p'

使用 GNU awk進行 FPAT(使用 Ed Morton 的代碼中的 regexp 和gensub function):

awk -v OFS=', ' -v FPAT='[[][^]]*][.][[][^]]*]' '{for (i=1; i<=NF; i++) printf "%s%s", gensub(/[][]/,"","g",$i), (i<NF?OFS:ORS)}' file
fox.jumps
the.quick, fox.jumps

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM