Splitting large text file on every blank line

后端 未结 9 905
南方客
南方客 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:45
    awk -v RS="\n\n" '{for (i=1;i<=NR;i++); print > i-1}' file.txt
    

    Sets record separator as blank line, prints each record as a separate file numbered 1, 2, 3, etc. Last file (only) ends in blank line.

    0 讨论(0)
  • 2020-12-05 08:47

    Setting RS to null tells awk to use one or more blank lines as the record separator. Then you can simply use NR to set the name of the file corresponding to each new record:

     awk -v RS= '{print > ("whatever-" NR ".txt")}' file.txt
    

    RS: This is awk's input record separator. Its default value is a string containing a single newline character, which means that an input record consists of a single line of text. It can also be the null string, in which case records are separated by runs of blank lines, or a regexp, in which case records are separated by matches of the regexp in the input text.

    $ cat file.txt
    dasdas #42319 blaablaa 50 50
    content content
    more content
    content conclusion
    
    asdasd #92012 blaablaa 30 70
    content again
    more of it
    content conclusion
    
    asdasd #299 yadayada 60 40
    content
    content
    contend done
    
    $ awk -v RS= '{print > ("whatever-" NR ".txt")}' file.txt
    
    $ ls whatever-*.txt
    whatever-1.txt  whatever-2.txt  whatever-3.txt
    
    $ cat whatever-1.txt 
    dasdas #42319 blaablaa 50 50
    content content
    more content
    content conclusion
    
    $ cat whatever-2.txt 
    asdasd #92012 blaablaa 30 70
    content again
    more of it
    content conclusion
    
    $ cat whatever-3.txt 
    asdasd #299 yadayada 60 40
    content
    content
    contend done
    $ 
    
    0 讨论(0)
  • 2020-12-05 08:49

    Try this bash script also

    #!/bin/bash
    i=1
    fileName="OutputFile_$i"
    while read line ; do 
    if [ "$line"  == ""  ] ; then
     ((++i))
     fileName="OutputFile_$i"
    else
     echo $line >> "$fileName"
    fi
    done < InputFile.txt
    
    0 讨论(0)
提交回复
热议问题