Split a text file by empty line or string pattern 16 February 2017
Say we have a file as below, we want to split this file by empty line.
Using awk, we can achieve that.
There is also a csplit command, which can do similar things.
man csplit to see specific usage of csplit. It may be a little confusing, at least for me. I can’t find a way to eliminate the empty line of the outfiles.
Similarly, if we want to split infile by specific string pattern instead of empty line, we can also achieve that.
Let’s try csplit first. This time, try to split infile2.txt by string “AAAA”.
Then, we try awk.
If you don’t like the trailing empty line of all the outfiles, use “printf” instead of “print”, like this.
In conclusion, to split file by empty line or specific string pattern, consider using awk or csplit. Personally I perfer awk, as it’s more flexible.