将文件的多行转换为每行一个字符串的文件

时间:2016-12-08 07:13:24

标签: shell unix

很抱歉问一个简单的问题,我有一个名为 file1 的文本文件,其中包含很多行句子。我想将带有空格的行分隔为分隔符,并将这些字符串打印为每行1个到另一个名为 file2 的文件中,如何在UNIX中执行此操作?

file1 的内容如下所示:

文件1

build targets target1 , target2, target3 in both windows machines
please rebuild target 4 build target5 as well as target6
target1 abc/saddggav/aafvfaddf/adfbdafdb/text1.msg
target2 dfdfbd/afbdahgtngs/adbadfdsbsdb/snstnhdb/text2.msg
target2
target6
please take all the files from dsvdfdfv/afedfgbadvbath/dsfbdsgfabadbafd/sdfbdgnsdb/dsfbwsgdb
please rebuild "build target4 and build target6 on sun machines

我想要排除其间的所有路径,但必须在行的开头取目标,并将所有单词打印为每1行1个字符串到另一个文件 file2

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

input.txt:
build targets target1 , target2, target3 in both windows machines
please rebuild target 4 build target5 as well as target6
target1 abc/saddggav/aafvfaddf/adfbdafdb/text1.msg
target2 dfdfbd/afbdahgtngs/adbadfdsbsdb/snstnhdb/text2.msg
target2
target6
please take all the files from dsvdfdfv/afedfgbadvbath/dsfbdsgfabadbafd/sdfbdgnsdb/dsfbwsgdb
please rebuild "build target4 and build target6 on sun machines

Code: cat input.txt | tr " " "\n" > output.txt

Output.txt:
build
targets
target1
,
target2,
target3
in
both
windows
machines
please
rebuild
target
4
build
target5
as
well
as
target6
target1
abc/saddggav/aafvfaddf/adfbdafdb/text1.msg
target2
dfdfbd/afbdahgtngs/adbadfdsbsdb/snstnhdb/text2.msg
target2
target6
please
take
all
the
files
from
dsvdfdfv/afedfgbadvbath/dsfbdsgfabadbafd/sdfbdgnsdb/dsfbwsgdb
please
rebuild
"build
target4
and
build
target6
on
sun
machines