如何使用sed从string中提取文本?
我的示例string如下所示:
This is 02G05 a test string 20-Jul-2012 
 现在从上面的string我想提取02G05 。 为此,我尝试了以下与sed的正则expression式 
 $ echo "This is 02G05 a test string 20-Jul-2012" | sed -n '/\d+G\d+/p' 
但是上面的命令不会打印任何东西,我相信它的原因是无法与我提供给sed的模式匹配。
所以,我的问题是我在这里做错了什么,以及如何纠正它。
当我用python试试上面的string和模式时,我得到了我的结果
 >>> re.findall(r'\d+G\d+',st) ['02G05'] >>> 
	
 模式\d可能不被你的sed支持。 试试[0-9]或[[:digit:]] 。 
要仅打印实际匹配(不是整个匹配行),请使用replace。
 sed -n 's/.*\([0-9][0-9]*G[0-9][0-9]*\).*/\1/p' 
 如何使用egrep ? 
 echo "This is 02G05 a test string 20-Jul-2012" | egrep -o '[0-9]+G[0-9]+' 
  sed不识别\d ,而是使用[[:digit:]] 。 您还需要转义+或使用-r开关(OS X上的-E )。 
 请注意[0-9]也适用于阿拉伯 – 印度教数字。 
试试这个:
 echo "This is 02G05 a test string 20-Jul-2012" | sed 's/.* \([0-9]\+G[0-9]\+\) .*/\1/' 
但是请注意,如果一行中有两个模式,则会打印第二个模式。
尝试使用rextract( https://github.com/kata198/rextract )
这将允许您使用正则expression式提取文本并重新格式化它。
例:
[$] echo“This is 02G05 a test string 20-Jul-2012”| ./rextract'([\ d] + G [\ d] +)''$ {1}'
2G05