简体   繁体   English

在两个词之间提取数据Linux脚本

[英]Extract data between two words Linux script

I have data in a file as shown below and need to extract the string between PER*IC and ~ PER*IC*FBI DEPT*EM*~ 我在一个文件中有数据,如下所示,需要提取PER*IC~ PER*IC*FBI DEPT*EM*~之间的字符串

I tried this: grep -P -o '(?<=PER*IC).*?(?=~)' inputfile but am getting an error message: 我尝试了这个: grep -P -o '(?<=PER*IC).*?(?=~)' inputfile但收到错误消息:

grep: lookbehind assertion is not fixed length grep:后置断言不是固定长度

When i tried grep -P -o '(?<=PER).*?(?=~)' inputfile it produces the output: *IC*FBI DEPT*EM* 当我尝试grep -P -o '(?<=PER).*?(?=~)' inputfile它会产生输出: *IC*FBI DEPT*EM*

I want to include PER*IC as start of string in code and expected result is *FBI DEPT*EM* 我想在代码中包括PER*IC作为字符串的开头,预期结果是*FBI DEPT*EM*

In '(?<=PER*IC).*?(?=~)' the '*' is a repetition character that will match zero or more 'R' characters. '(?<=PER*IC).*?(?=~)''*'是一个重复字符,它将与零个或多个 'R'字符匹配。 You need a literal '*' which can be done by including it within a character class, eg [*] making your expression: 您需要一个文字'*' ,可以通过将其包含在字符类中来完成,例如[*]进行表达式:

grep -P -o '(?<=PER[*]IC).*?(?=~)'

Which would return: 哪个会返回:

*FBI DEPT*EM*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM