我有两行字符串:
> a="Microarchitectural Data Sampling (MDS) aka CVE-2018-12126, CVE-2018-12127,CVE-2018-12130, CVE-2019-11091, publicly announced by Intel on 5/14/2019, this has high visibility and lots of public media exposure.\nMicroarchitectural Data Sampling (MDS) aka CVE-2018-12126, CVE-2018-12127,CVE-2018-12130, CVE-2019-11091, publicly announced by Intel on 5/14/2019, this has high visibility and lots of public media exposure."
> echo -e $a
Microarchitectural Data Sampling (MDS) aka CVE-2018-12126, CVE-2018-12127,CVE-2018-12130, CVE-2019-11091, publicly announced by Intel on 5/14/2019, this has high visibility and lots of public media exposure.
Microarchitectural Data Sampling (MDS) aka CVE-2018-12126, CVE-2018-12127,CVE-2018-12130, CVE-2019-11091, publicly announced by Intel on 5/14/2019, this has high visibility and lots of public media exposure.
我要打印的是:
CVE-2018-12126 CVE-2018-12127 CVE-2018-12130 CVE-2019-11091
CVE-2018-12126 CVE-2018-12127 CVE-2018-12130 CVE-2019-11091
# OR
CVE-2018-12126
CVE-2018-12127
CVE-2018-12130
CVE-2019-11091
CVE-2018-12126
CVE-2018-12127
CVE-2018-12130
CVE-2019-11091
我在下面尝试过
> echo -e $a | sed -r 's/.*(CVE-[0-9]{4}-[0-9]{4,6}).*/\1/g'
CVE-2019-11091
CVE-2019-11091
仅打印每行的最后一个匹配项:-)
如何打印所有匹配的组?
答案 0 :(得分:2)
将grep
与-o
选项一起使用,将仅输出匹配的子字符串:
grep -o 'CVE-[0-9]\{4\}-[0-9]\{4,6\}' file > outputfile
请注意,\{4\}
中的花括号已转义,因为这是默认的POSIX BRE引擎兼容正则表达式。
使用sed,简单的解决方案是使用两个步骤:用换行符包装预期的匹配,然后提取与您的模式完全匹配的匹配:
pat='CVE-[0-9]\{4\}-[0-9]\{4,6\}'
sed "s/$pat/\n&\n/g" file.txt | sed -n "/^$pat\$/p" > outputfile
输出:
CVE-2018-12126
CVE-2018-12127
CVE-2018-12130
CVE-2019-11091
CVE-2018-12126
CVE-2018-12127
CVE-2018-12130
CVE-2019-11091
请参见online demo
答案 1 :(得分:1)
这可能对您有用(GNU sed):
sed -E '/\n/!s/CVE-[0-9]{4}-[0-9]{4,6}/\n&\n/g;/^CVE-[0-9]{4}-[0-9]{4,6}/P;D' file
用换行符包围所需的字符串,然后仅打印这些行。
或者,如果您愿意:
regexp='CVE-[0-9]\{4\}-[0-9]\{4,6\}'
sed '/\n/!s/'$regexp'/\n&\n/g;/^'$regexp'/P;D' file