【问题标题】:converting multi lines of a file a file with single strings per line将文件的多行转换为每行单个字符串的文件
【发布时间】:2016-12-09 10:58:37
【问题描述】:

很抱歉问一个简单的问题,我有一个名为 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

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你的问题似乎可以用cat a.txt | tr ' ' \012 > b.txt来完成
  • 感谢 Marichyasana
  • 我试过 cat a.txt | tr ' ' \012 > b.txt 这个命令,这是在字符串之间的空格中加0
  • 试试"$( printf '\012' )"而不是\012
  • 看起来您在我回答后更改了输入文件,现在是 8 行,而之前是 1 行。

标签: shell unix


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2019-12-14
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多