2013-10-08 27 views
0

我想使用sed刪除一些行的開頭。目標是在每行中刪除所有字符,直到出現兩個連續的大寫字母。使用sed刪除一行的開頭,直到大寫字

的投入將始終類似於:

1 where did you get ACQUIRE, obtain, come by, receive, gain, earn, win, come into, take 
2 I got your letter: RECEIVE, be sent, be in receipt of, be given. 
3 your tea is getting cold: BECOME, grow, turn, go. 
4 get the children from school: FETCH, collect, go for, call for, pick up, bring, deliver, convey, ferry, transport. 
5 the chairman gets £650,000 a year: EARN, be paid, take home, bring in, make, receive, collect, gross; informal pocket, bank, rake in, net, bag. 
6 have the police got their man?: APPREHEND, catch. 

而且我所要的輸出是:

ACQUIRE, obtain, come by, receive, gain, earn, win, come into, take 
RECEIVE, be sent, be in receipt of, be given. 
BECOME, grow, turn, go. 
FETCH, collect, go for, call for, pick up, bring, deliver, convey, ferry, transport. 
EARN, be paid, take home, bring in, make, receive, collect, gross; informal pocket, bank, rake in, net, bag. 
APPREHEND, catch. 

我建立這個:

sed -n 's/^.*[A-Z]\{2\}//p' 

但這表達式也會刪除大寫的單詞。有關如何做到這一點的任何線索?

回答

1

的問題是缺乏預見性和非貪婪選項。解決這個問題的一種方法是做兩個替換。第一個就是你想要的文字,將其保存爲第1組和一個換行符後追加,然後刪除所有數據,直到換行符,就像這樣:

sed 's/\([A-Z]\{2,\}.*\)/\n\1/; s/[^\n]*\n//' infile 

它產生:

ACQUIRE, obtain, come by, receive, gain, earn, win, come into, take 
RECEIVE, be sent, be in receipt of, be given. 
BECOME, grow, turn, go. 
FETCH, collect, go for, call for, pick up, bring, deliver, convey, ferry, transport. 
EARN, be paid, take home, bring in, make, receive, collect, gross; informal pocket, bank, rake in, net, bag. 
APPREHEND, catch. 
1

這應該awk工作過,但它給上線的輸出錯誤5

awk '{print substr($0,match($0,/[[:upper:]][[:upper:]]/))}' file 
ACQUIRE, obtain, come by, receive, gain, earn, win, come into, take 
RECEIVE, be sent, be in receipt of, be given. 
BECOME, grow, turn, go. 
FETCH, collect, go for, call for, pick up, bring, deliver, convey, ferry, transport. 
5 the chairman gets 
APPREHEND, catch. 

match找到兩個第一upperc ase letter,那麼substr使用它來打印該行的最後部分。