Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 转换输入文件中的字符串_String_Unix - Fatal编程技术网

String 转换输入文件中的字符串

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

我在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. 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