转换输入文件中的字符串

时间:2012-12-01 20:08:07

标签: string unix

我在Unix上有一个文本文件file1.txt。我想制作另一个文件file2.txt,其中我更改了具有此格式的所有行组(取自多项选择考试)

a. [first choice]
b. [second choice]
c. [third choice]

[first choice] [second choice] [third choice]

我怎么能这样做?

编辑:一个例子是

What is the value of three plus five?
a. six
b. seven
c. eight

This line is not so relevant.
blah blah

What is the capital of England?
a. London
b. Birmingham
c. New York

应该转换为

What is the value of three plus five?
six seven eight

This line is not so relevant.
blah blah

What is the capital of England?
London Birmingham New York    

1 个答案:

答案 0 :(得分:3)

这个单行应该适合你:

 awk '{if($0~/^[a-z]\. /){gsub(/^[a-z]\. /,"");printf "%s ",$0;s=1;next;}else{if(s)print "";print $0;s=0}}' file1

测试

kent$  cat exam
What is the value of three plus five?
a. six
b. seven
c. eight

This line is not so relevant.
blah blah

What is the capital of England?
a. London
b. Birmingham
c. New York

kent$  awk '{if($0~/^[a-z]\. /){gsub(/^[a-z]\. /,"");printf "%s ",$0;s=1;next;}else{if(s)print "";print $0;s=0}}' exam
What is the value of three plus five?
six seven eight 

This line is not so relevant.
blah blah

What is the capital of England?
London Birmingham New York