【问题标题】:separate file into several将文件分成几个
【发布时间】:2014-09-11 17:24:48
【问题描述】:

我有一个包含多个段落的文本文件,我想将其分割成单独的文件。它应该适用于 Linux 系统。

例如: 首先我有这个文件:

== 01.01.2014 ==
this is the text that should 
go into

file one
and i like file one to be namend 01-01-2014

== 15.02.2014 ==
and this is the text that should
end up 
in file two

where file two is named 
15-02-2014

并且切割它应该有两个文件,名为 15-02-2014.txt 和 01-01-2014.txt 仅包含属于日期的文本部分,一个问题可能是中间有换行符和空行, 所以一个文件可以包含多个文本块

我认为使用 awk 会有一个解决方案,但我不知道如何将其保存到不同的文件中。 我的另一种方法是使用 libreoffice writer,但它的搜索和替换功能似乎不允许插入分页符。

【问题讨论】:

    标签: file awk cut


    【解决方案1】:

    你可以这样做:

    awk '/^==/ {out=$2} {print > out".txt"}' file
    

    它将使用==之后的日期作为输出文件的名称。

    【讨论】:

    • 非常感谢!那是完美的。对不起,我没有足够的声誉来支持它。只要我被允许就会接受它
    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多