简体   繁体   English

在AWK或SED中找到带有正则表达式的字符串

[英]Found string with regex in AWK or SED

I want to find for example the following string 9Stest1.test2D9 in File. 例如,我想在File中找到以下字符串9Stest1.test2D9。 Then i want to cut the first 2 charechter and last 2 charechter and finally print the text befor and after . 然后我要剪切前2个字符,最后2个字符,最后在befor及其之后打印文本。 in two seperate Line. 在两条分开的线。

Example Text : 范例文字:

7U8vTest(#G-HLjYM6QqJj1j"7MFx$^Qd
.f@alU|A#Z<inCWV6a=L?o`A5vIod"%Mm+YW1RM@,L;aN
r^n<&)}[??!VcVIV**9Stest1.test2D9**94EN~yK,$lU=9?UT.[
e`)G:FS.nGz%?@~k!20aLJ^PU-[@}0W\ !8x
cujOmEK"1;!cI134lu%0-A +/t!VIf?8uT`!
aC1QAQY>4RE$46iVjAE^eo5yR|
1?/T?<H5,%G~[|9I/c&8MY$O]%,UYQe{!{Bm[rRC[
aHC`<m?BUau@N_O>Yct.MXo[>r5^uV&26@MkYB'Kiu\Y
K(*}ldO:ZQnI8t989fi+

Output should be so : 输出应为:

test1
test2

I have tried with following code grep "[0-9][a-zA-Z]\\+\\.[a-zA-Z]\\+[0-9]" to find the string.Now i can cut the firs two line with cut command but last two line? 我已经尝试使用以下代码grep "[0-9][a-zA-Z]\\+\\.[a-zA-Z]\\+[0-9]"来查找字符串。现在我可以切割冷杉了两行带cut command但最后两行? I think with AWK can i solve my problem easily but i dont know how. 我认为使用AWK可以轻松解决我的问题,但我不知道如何解决。 Thanks 谢谢

see this line if it helps: 如果有帮助,请参阅此行:

kent$  grep -Po '(?<=9S).*?(?=D9)' file
test1.test2

or more dynamic, this works for your example: 或更动态,这适用于您的示例:

kent$  grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' file
test1.test2

EDIT 编辑

as @devnull suggested, to get the desired output, you could 如@devnull建议,要获得所需的输出,您可以

grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' file|tr '.' '\n'

This depends on how and what to search for. 这取决于搜索方式和搜索内容。 Like text between ** **之间的文字

awk -F"**"  '{a=substr($2,3,length($2)-4)} a {print a}'
test1.test2
dO:ZQnI8t989f

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

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