2014-09-02 25 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