转换输入文件中的字符串

转换输入文件中的字符串

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

假设总是有 3 个选择,a.b.c.试试这个:

sed '/^[a-c]\. /{N;N;s/[a-c]\. / /g;s/[\r\n]//g;s/^ //}' file1.txt > file2.txt

其工作原理是,使用命令一次抓取三行N,用空格替换所有出现的 、a.和,删除所有行尾,最后删除行首剩余的额外空格。b.c.

相关内容