搜索带有特殊字符linux

时间:2018-05-01 04:43:05

标签: regex awk

我是bash脚本的新手。我试图从文件中获取与某些模式匹配的单词。但是,我无法从中得到任何东西。我认为问题在于特殊字符。有人可以帮我找到解决方案。

示例:

enter image description here

我的代码:

awk '/[c8d313c81caa1103:-603ec52f:1630466b4e8:5448][amirbekova] com.sas.analytics.crm.custdata.sql/{flag=1;next}/quit;/{flag=0}flag && /CMDM\./' SASMarketingAutomationCore5.4.log.2018-04-27 >> test1.txt

所以我正在寻找CMDM。介于两种模式之间的词:

[c8d313c81caa1103:-603ec52f:1630466b4e8:5448][amirbekova] com.sas.analytics.crm.custdata.sql

quit;

输入:

[c8d313c81caa1103:-603ec52f:1630466b4e8:5448][amirbekova] com.sas.analytics.crm.custdata.sql - TID[25810F8F] \namirbekova----- Execute Sql in MAIQService.execute() ----- \nPROC SQL; \nCreate table MATables.TXIDFAEAIZ5BDSAAGamirbekova as \nSELECT DISTINCT table0.DWH_ID AS DWH_ID LABEL='Идентификатор клиента в КИХ' FORMAT=$40. \nFROM \nCMDM.INDIVIDUAL_OBLIGATIONS table2 Left join CMDM.SYS_DICT_BINARY table1 on ( ( (table2.IO_ACTIVE_SIGN=table1.ID)) ) \nRight join CMDM.CLIENT table0 on ( ( (table0.DWH_ID=table2.CLIENT _DWH_ID)) ) quit;

输出:

- TID[25810F8F] amirbekova----- Execute Sql in MAIQService.execute() ----- PROC SQL; Create table MATables.TXIDFAEAIZ5BDSAAGamirbekova as SELECT DISTINCT table0.DWH_ID AS DWH_ID LABEL='Идентификатор клиента в КИХ' FORMAT=$40. FROM CMDM.INDIVIDUAL_OBLIGATIONS table2 Left join CMDM.SYS_DICT_BINARY table1 on ( ( (table2.IO_ACTIVE_SIGN=table1.ID)) ) Right join CMDM.CLIENT table0 on ( ( (table0.DWH_ID=table2.CLIENT _DWH_ID)) )

2 个答案:

答案 0 :(得分:1)

您可以在文件/变量上运行以下grep命令。

grep -oP '(?<=\[c8d313c81caa1103:-603ec52f:1630466b4e8:5448]\[amirbekova] com\.sas\.analytics\.crm\.custdata\.sql)(.*)(?=quit;)' ~/test_awk/input_file

<强>说明:

  • -oP -o仅显示匹配的正则表达式输出而不显示整行,-P为lookbehind / lookahead激活perl正则表达式,...

  • 以下正则表达式(?<=\[c8d313c81caa1103:-603ec52f:1630466b4e8:5448]\[amirbekova] com\.sas\.analytics\.crm\.custdata\.sql)(.*)(?=quit;)符合您的模式:demo:https://regex101.com/r/FFrCu3/1/和 关于正则表达式的解释:http://www.rexegg.com/regex-quickstart.html

<强>样本:

$ cat ~/test_awk/input_file
[c8d313c81caa1103:-603ec52f:1630466b4e8:5448][amirbekova] com.sas.analytics.crm.custdata.sql - TID[25810F8F] amirbekova----- Execute Sql in MAIQService.execute() ----- PROC SQL; Create table MATables.TXIDFAEAIZ5BDSAAGamirbekova as SELECT DISTINCT table0.DWH_ID AS DWH_ID LABEL='Идентификатор клиента в КИХ' FORMAT=$40. FROM CMDM.INDIVIDUAL_OBLIGATIONS table2 Left join CMDM.SYS_DICT_BINARY table1 on ( ( (table2.IO_ACTIVE_SIGN=table1.ID)) ) Right join CMDM.CLIENT table0 on ( ( (table0.DWH_ID=table2.CLIENT _DWH_ID)) ) quit;

$ grep -oP '(?<=\[c8d313c81caa1103:-603ec52f:1630466b4e8:5448]\[amirbekova] com\.sas\.analytics\.crm\.custdata\.sql)(.*)(?=quit;)' ~/test_awk/input_file
 - TID[25810F8F] amirbekova----- Execute Sql in MAIQService.execute() ----- PROC SQL; Create table MATables.TXIDFAEAIZ5BDSAAGamirbekova as SELECT DISTINCT table0.DWH_ID AS DWH_ID LABEL='Идентификатор клиента в КИХ' FORMAT=$40. FROM CMDM.INDIVIDUAL_OBLIGATIONS table2 Left join CMDM.SYS_DICT_BINARY table1 on ( ( (table2.IO_ACTIVE_SIGN=table1.ID)) ) Right join CMDM.CLIENT table0 on ( ( (table0.DWH_ID=table2.CLIENT _DWH_ID)) ) 

如果你的模式在几行上,那么你可以使用以下awk命令:

awk '/^\[c8d313c81caa1103:-603ec52f:1630466b4e8:5448\]\[amirbekova\] com.sas.analytics.crm.custdata.sql/{flag=1;print $NF;next}/^quit;/{flag=0;}{if(flag){print}}' input_file

答案 1 :(得分:0)

[]在正则表达式中很特殊,需要进行转义:\[

第一种模式中的点相同:com\.sas\.等。