awksplitcsplit

Bash how to split file on empty line with awk


I have a text file (A.in) and I want to split it into multiple files. The split should occur everytime an empty line is found. The filenames should be progressive (A1.in, A2.in, ..)

I found this answer that suggests using awk, but I can't make it work with my desired naming convention

awk -v RS="" '{print $0 > $1".txt"}' file

I also found other answers telling me to use the command csplit -l but I can't make it match empty lines, I tried matching the pattern '' but I am not that familiar with regex and I get the following

bash-3.2$ csplit A.in ""
csplit: : unrecognised pattern

Input file:

A.in

4 
RURDDD

6
RRULDD
KKKKKK

26
RRRULU

Desired output:

A1.in

4 
RURDDD

A2.in

6
RRULDD
KKKKKK

A3.in

26
RRRULU

Solution

  • Another fix for the awk:

    $ awk -v RS="" '{
        split(FILENAME,a,".")  # separate name and extension
        f=a[1] NR "." a[2]     # form the filename, use NR as number
        print > f              # output to file
        close(f)               # in case there are MANY to avoid running out f fds
    }' A.in