2014-09-02 28 views
0

说我有输出的几行看起来像这样:AWK:斩根据正则表达式的东西断行首

blah <foo> I want this 
baz < nom> I want this too 
bit <@hi> And this... 

如何使用AWK之前砍掉一切,包括,第一“>”每行上的字符?

+0

Can there there can在一行上多于一个'>'? – 2014-09-02 23:55:49

+0

是的,可以。按照jaypal的回答,我最终使用了sed。 – felamaslen 2014-09-03 01:37:06

+0

一个很好的选择我会用'cut'自己,但是jaypals second sed也不错。我很好奇 - 为什么你特别要求awk解决方案(然后接受sed答案)? – 2014-09-03 15:14:03

回答

2

让不忘记切断,这是它发明的:

cut -d\> -f2- file 
+1

Unix理念的完美例子:)接受,因为它比awk更好。 – felamaslen 2014-09-03 16:47:21

0

使用awk的,你可以这样做:

awk '{sub(/^[^.]*>/, "");} 1' file 
I want this 
I want this too 
    And this... 

或者使用SED:

sed 's/^[^.]*>//' file 
I want this 
I want this too 
    And this... 
2

如果你只有>性格,一旦你可以做一个简单sed替代:

sed 's/.*>//' file 

如果可以有很多上面的贪婪(*)会消耗一切直到最后>字符。在这种情况下,你最好这样做:

sed 's/[^>]*>//' file 
2

这可能会做(如果你有一个>

awk -F\> '{print $2}' file 
I want this 
I want this too 
    And this... 
+0

工作正常!辉煌。 – felamaslen 2014-09-02 18:48:48

0

试试这个:

awk -F">" '{print $1">"}' filename