从线上提取模式

时间:2014-11-05 12:18:24

标签: shell design-patterns extract cat

如何提取包含模式的文字" rum "从以下一行:

Agarum anoestrum  alabastrum sun antirumor alarum antiserum ambulacrum antistrumatic Anatherum antistrumous androphorum antrum 4. foodstuff foody nonfood Aplectrum 

所以应该删除像sun,4.,foody,nonfood这样的词语。

3 个答案:

答案 0 :(得分:0)

您可以使用grep

echo "Agarum anoestrum sun" | tr ' ' '\n' | grep "arum"

tr用于将输入字符串分成每行一个单词,因为grep按行进行操作并显示整行。
如果您希望输出再次在一行中,请使用:

echo "Agarum anoestrum sun" | tr ' ' '\n' | grep "arum" | tr '\n' ' '

答案 1 :(得分:0)

使用grep -Eo

grep -Eo 'a[[:alnum:]]*rum' file
arum
anoestrum
alabastrum
antirum
alarum
antiserum
ambulacrum
antistrum
atherum
antistrum
androphorum
antrum

答案 2 :(得分:0)

尝试:

echo Agarum anoestrum alabastrum sun antirumor alarum antiserum ambulacrum antistrumatic Anatherum antistrumous androphorum antrum 4. foodstuff foody nonfood Aplectrum | awk '{for (i=1;i<NF;i++) { if (match($i, "[aA][[:alnum:]]*[rR][uU][mM]") != 0) { printf ("%s ", $i) }} print}'