String 转换输入文件中的字符串
我在Unix上有一个文本文件String 转换输入文件中的字符串,string,unix,String,Unix,我在Unix上有一个文本文件file1.txt。我想生成另一个文件file2.txt,在该文件中,我更改了具有此格式的所有行组(取自多项选择题考试) 到 我怎么能这么做 编辑:例如 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. Birmingha
file1.txt
。我想生成另一个文件file2.txt
,在该文件中,我更改了具有此格式的所有行组(取自多项选择题考试)
到
我怎么能这么做
编辑:例如
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
这一行应该适合您:
awk '{if($0~/^[a-z]\. /){gsub(/^[a-z]\. /,"");printf "%s ",$0;s=1;next;}else{if(s)print "";print $0;s=0}}' file1
试验
你的输入完成了吗?那么文件2将只包含一行?把所有的[…]连在一起?file1中的“组”看起来如何?@Kent抱歉,我的描述似乎不清楚。请看我的例子。
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
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