嘗試撰寫正則運算式來捕獲給定的字母數字值,但它也捕獲其他數值。獲得欲望輸出的正確方法應該是什么?
代碼
grep -Eo '(\[[[:alnum:]]\)\w ' file > output
$ cat file
2022-04-29 08:45:11,754 [14] [Y23467] [546] This is a single line
2022-04-29 08:45:11,764 [15] [fpes] [547] This is a single line
2022-04-29 08:46:12,454 [143] [mwalkc] [548] This is a single line
2022-04-29 08:49:12,554 [143] [skhat2] [549] This is a single line
2022-04-29 09:40:13,852 [5] [narl12] [550] This is a single line
2022-04-29 09:45:14,754 [1426] [Y23467] [550] This is a single line
電流輸出 -
[14
[Y23467
[546
[15
[fpes
[547
[143
[mwalkc
[548
[143
[skhat2
[549
[5
[narl12
[550
[1426
[Y23467
[550
預期輸出 -
Y23467
fpes
mwalkc
skhat2
narl12
Y23467
uj5u.com熱心網友回復:
第一種解決方案:使用您顯示的示例,請嘗試以下awk代碼。簡單的解釋是,使用gsub函式替換[并]在第 4 個欄位中,然后列印第 4 個欄位。
awk '{gsub(/\[|\]/,"",$4);print $4}' Input_file
第二種解決方案:使用 GNUgrep請嘗試以下解決方案。
grep -oP '^[0-9]{4}(-[0-9]{2}){2} [0-9]{2}(:[0-9]{2}){2},[0-9]{1,3} \[[0-9] \] \[\K[^]]*' Input_file
說明:為 GNU 中使用的上述正則運算式添加詳細說明grep。
^[0-9]{4}(-[0-9]{2}){2} ##From starting of value matching 4 digits followed by dash 2 digits combination of 2 times.
[0-9]{2}(:[0-9]{2}){2} ##Matching space followed by 2 digits followed by : 2 digits combination of 2 times.
,[0-9]{1,3} ##Matching comma followed by digits from 1 to 3 number.
\[[0-9] \] \[\K ##Matching space followed by [ digits(1 or more occurrences of digits) followed by space [ and
##then using \K to forget all the previously matched values.
[^]]* ##Matching everything just before 1st occurrence of ] to get actual values.
uj5u.com熱心網友回復:
使用[[:alnum:]]or\w表示它可能匹配字母數字或單詞字符。
如果可以有數字,但應該有一個字符 az 并且-P支持使用 perl 兼容的正則運算式:
grep -oP '\[\K\d*[A-Za-z][\dA-Za-z]*(?=])' file
解釋
\[匹配[\K忘記到目前為止匹配的內容\d*[A-Za-z]匹配可選數字和至少一個字符 a-zA-Z[\dA-Za-z]*匹配可選字符 a-zA-Z 和數字(?=])]向右斷言
輸出
Y23467
fpes
mwalkc
skhat2
narl12
Y23467
如果只能出現 1 次,您也可以將 sed 與捕獲組一起\(...\)使用,并在替換中使用該組\1
sed 's/.*\[\([[:digit:]]*[[:alpha:]][[:alnum:]]*\)].*/\1/' file
uj5u.com熱心網友回復:
您的問題有幾個部分。首先,我將嘗試幫助您處理正則運算式(但它可能會解決更多問題);接下來我會告訴你一個替代方案。
正則運算式
要了解[[:alnum:]]的是它捕獲任何包含字母數字字符的內容。所以它會捕獲“123”,它會捕獲“abc”,因為所有這些字符都是字母數字。它會單獨判斷每個字符,并且無法像您想要的那樣捕獲“僅具有數字和字母的部分”。
但是,通過將幾個greps 鏈接在一起,我們可以過濾掉只包含數字的行。
grep -Eo '(\[[[:alnum:]]\)\w ' file | grep -v -Eo '\[[[:digit:]] (\w |$)' > output
為了進一步完善這一點,您的正則運算式中似乎存在一些錯誤。首先,您已將\[捕獲的部分包含在內部,這就是它[在結果中捕獲的原因,因此您應該更改(\[為將捕獲部分\[(的外部移動到括號中。[( ... )
接下來,您的[[:alnum:]]with組合\w 可能不會達到您的預期。它查找單個字母數字字符,后跟一個或多個“單詞”字符(這是所有字母數字,加上一些額外的)。你可能想要([[:alnum:]] )而不是([[:alnum:]])\w
選擇
為什么不cut改用?cut -d' ' -f4將采用第 4 個欄位(以“空格”作為欄位之間的分隔符)
$ cut -d' ' -f 4 file
[Y23467]
[fpes]
[mwalkc]
[skhat2]
[narl12]
[Y23467]
如果您還想洗掉方括號,請嘗試
$ cut -d' ' -f 4 file | grep -Eo '\w '
Y23467
fpes
mwalkc
skhat2
narl12
Y23467
uj5u.com熱心網友回復:
使用sed
$ sed 's/\([^[]*\[\)\{2\}\([^]]*\).*/\2/' input_file
Y23467
fpes
mwalkc
skhat2
narl12
Y23467
uj5u.com熱心網友回復:
FPAT與 GNU 一起使用awk:
awk -v FPAT='[[[:alnum:]]*]' '{gsub(/^\[|\]$/, "",$(NF-1));print $(NF-1)}' file
Y23467
fpes
mwalkc
skhat2
narl12
Y23467
設定
FPAT為'[[[:alnum:]]*]'我們匹配[char 后跟零個或多個字母數字字符后跟]char。使用
gsub()函式我們洗掉初始[和最終]字符。我們列印最后一個欄位之前的欄位,即
$(NF-1)欄位,沒有[和]字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468187.html
