Splitting large text file on every blank line

后端 未结 9 953
南方客
南方客 2020-12-05 07:53

I\'m having a bit trouble of splitting a large text file into multiple smaller ones. Syntax of my text file is the following:



        
9条回答
  •  难免孤独
    2020-12-05 08:34

    You could use the csplit command:

    csplit \
        --quiet \
        --prefix=whatever \
        --suffix-format=%02d.txt \
        --suppress-matched \
        infile.txt /^$/ {*}
    

    POSIX csplit only uses short options and doesn't know --suffix and --suppress-matched, so this requires GNU csplit.

    This is what the options do:

    • --quiet – suppress output of file sizes
    • --prefix=whatever – use whatever instead fo the default xx filename prefix
    • --suffix-format=%02d.txt – append .txt to the default two digit suffix
    • --suppress-matched – don't include the lines matching the pattern on which the input is split
    • /^$/ {*} – split on pattern "empty line" (/^$/) as often as possible ({*})

提交回复
热议问题