sh: split a file rocks

woman standing in between split gateway

sh: split a file rocks

While you can do this probably with sed or awk, split is definitely easier. A hat tip to @guy for telling us all about it. Basically, if you want to split a file into chunks of 1,000 lines, it will do this and add a prefix, so if you have log.txt, then split log.txt will get you xaa, xab,... There is a long tutorial on this and the syntax is a little weird.

# Every fifth line is kept
awk 'NR % 5 == 0' log.txt > log5.txt
sed -n '0~5p' log.txt > log5.txt
# split into 8 line blocks
split -l 8 log.txt
# split -n r/i/n means block into n lines and take the ith in that block
# take the 1st line in each group of 3 lines
split -n r/1/3

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: