我想知道是否有一种通用的方法来提取特定的字符串,根据设计,它是一个使用awk方法的11个字符字母数字字符串?对前-
cat ext.txt
This is a sample field where the code is MGTCBEBEECL for NR
This is a sample field where the code is MGTCBEBEE01 for NR
This field must be 030 when Rule_1 = 'FR' and Rule_2 is 'EUROFRANSBI' or 'EURO_NEAR' and code is PARBFRPPXXX
This field must be 0186 when Rule_1 = 'FR' and Rule_2 is 'EUROFRANSBI' or 'EURO_NEAR' and code is CITIFRPPXXX for the NR
For NFNC with Rule_1 is CA and Rule_2 is Universal and business code is null and official code must be 'CIBCCATTXXX'我只想提取密码:-
MGTCBEBEECL
MGTCBEBEE01
PARBFRPPXXX
CITIFRPPXXX
CIBCCATTXXX几乎有100行这样的线,我希望从中提取这些不同的字符串,但我在我的智慧结束如何使它更通用和非多余,因此寻求这个社会的帮助!
发布于 2022-04-11 10:25:00
使用当前的示例,您可以使用这样的grep来完成它:
<ext.txt grep -oE "(code is|code must be) '?[A-Z0-9]{11}'?" |
tr -d "'" |
grep -o '[^ ]*$'输出:
MGTCBEBEECL
MGTCBEBEE01
PARBFRPPXXX
CITIFRPPXXX
CIBCCATTXXX发布于 2022-04-11 13:13:37
GNU awk有一种使用FPAT的方法:
awk -v FPAT='[[:alnum:]]{11}' '{print $NF}' file
MGTCBEBEECL
MGTCBEBEE01
PARBFRPPXXX
CITIFRPPXXX
CIBCCATTXXX将FPAT设置为characters.
{print $NF},用于打印所需的字段。发布于 2022-04-11 10:42:08
使用gawk:
gawk -F "[ ']" 'BEGIN{ r=@/[A-Z]{11}/ }r{ for (i=1; i<=NF;i++){ if($i~r) print $i} }' ext.txt-F "[ ']"使用空格或'作为字段分隔符(还可以找到像'CIBCCATTXXX')r=@/[A-Z]{11}/这样的代码,指定已使用的正则表达式(因为在一行的所有字段上scriptfor(...循环中使用了两次),并在匹配正则表达式时打印字段。产出:
MGTCBEBEECL
EUROFRANSBI
PARBFRPPXXX
EUROFRANSBI
CITIFRPPXXX
CIBCCATTXXXhttps://stackoverflow.com/questions/71826058
复制相似问题