包含字符串的单词的grep或sed

时间:2011-11-09 12:09:13

标签: regex sed grep

示例文件:

blahblah 123.a.site.com   some-junk
yoyoyoyo 456.a.site.com   more-junk
hihohiho 123.a.site.org   junk-in-the-trunk
lalalala 456.a.site.org   monkey-junk

我想在每一行的中间插入所有这些域,它们都有一个共同的部分a.site,我可以用它来寻找,但我无法确定如何在不返回的情况下完成它整条线?

这里可能需要sed或正则表达式,因为简单的grep是不够的?

2 个答案:

答案 0 :(得分:9)

你可以这样做:

grep -o '[^ ]*a\.site[^ ]*' input

awk '{print $2}' input

sed -e 's/.*\([^ ]*a\.site[^ ]*\).*/\1/g' input

答案 1 :(得分:3)

尝试此操作以查找该位置的任何内容

  $ sed -r "s/.* ([0-9]*)\.(.*)\.(.*)/\2/g"

 [0-9]* - For match number zero or more time.
 .*     - Match anything zero or more time.
 \.     - Match the exact dot.
 ()     - Which contain the value particular expression in parenthesis, it can be printed using \1,\2..\9. It contain only 1 to 9 buffer space. \0 means it contain all the expressed pattern in the expression.