在AWK或SED中找到带有正则表达式的字符串 [英] Found string with regex in AWK or SED
问题描述
例如,我想在File中找到以下字符串9Stest1.test2D9.然后我要剪切前2个字符,最后2个字符,最后在befor和after之后打印文本.在两条单独的线中.
I want to find for example the following string 9Stest1.test2D9 in File. Then i want to cut the first 2 charechter and last 2 charechter and finally print the text befor and after . in two seperate Line.
示例文字:
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+
输出应为:
test1
test2
我尝试使用下面的代码grep "[0-9][a-zA-Z]\+\.[a-zA-Z]\+[0-9]"
查找字符串.现在我可以用cut command
剪切出第一行,但是最后两行呢?
我认为使用AWK
可以轻松解决我的问题,但我不知道如何解决.
谢谢
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?
I think with AWK
can i solve my problem easily but i dont know how.
Thanks
推荐答案
查看此行是否有帮助:
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
编辑
如@devnull所建议的那样,要获得所需的输出,您可以
as @devnull suggested, to get the desired output, you could
grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' file|tr '.' '\n'
这篇关于在AWK或SED中找到带有正则表达式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!