从unix中从特定单词到最后一列的行中选择列?

时间:2013-07-09 06:48:49

标签: regex perl sed awk cut

输入文件

1. blue is color
2. His shirt color is blue and it is good
3. deep blue see is a movie

输出:

1. blue is color
2. blue and it is good
3. blue see is a movie

我需要输出从unix中的特定单词到最后一列,使用awk或cut。

4 个答案:

答案 0 :(得分:4)

awk 'BEGIN{FS="\\<blue\\>"; OFS="blue"}{$1=""}7' file

以上行将输出:

kent$  awk 'BEGIN{FS="\\<blue\\>"; OFS="blue"}{$1=""}7' file                                                                                                                   
blue is color
blue and it is good
blue see is a movie

请注意,"\\<blue\\>"将完全匹配单词blue,而不是bluesky or darkblue我希望这是您想要的。

答案 1 :(得分:3)

的代码:

$ sed -r 's/^([0-9]+. ).* (blue)\b/\1\2/' file
1. blue is color
2. blue and it is good
3. blue see is a movie

答案 2 :(得分:1)

这是一个很常见的用例,

cat test.txt | awk '{ if (x=index($0,"blue")) { print substr($0,x,length($0)); } }'

我建议你看看Awk - A Tutorial and Introduction

答案 3 :(得分:0)

Perl:

perl -nle'/(blue.*)/&&print"$1\n"'