2

file1.txt在 Unix 上有一个文本文件。我想生成另一个文件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    
4

1 回答 1

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  
于 2012-12-01T20:36:42.233 回答