I have a 10000 lines file.I want to create batches of 100 lines at a time in each different file.I know that this can be done using a while loop.But, I want to know whether it can be directly done using a sed or awk or head or tail commands only
3 Answers
Use split
-- this is why that command was created
split -l 100 big_file
You can use awk too:
awk -v n=100 'NR%n == 1 {out = "outfile" ++c} {print > out}' big_file
Danilas-MacBook-Pro:ruby dladner$ head sample.txt
testing
testing
testing
testing
testing
testing
testing
testing
testing
testing
Danilas-MacBook-Pro:ruby dladner$ wc -l sample.txt
10000 sample.txt
Danilas-MacBook-Pro:ruby dladner$ awk 'NR%100==1{fn && close(fn);fn="FILE"++i".txt";}{print > fn}' sample.txt
Danilas-MacBook-Pro:ruby dladner$ head FILE1.txt
testing
testing
testing
testing
testing
testing
testing
testing
testing
testing
Danilas-MacBook-Pro:ruby dladner$ wc -l !$
wc -l FILE1.txt
100 FILE1.txt
Danilas-MacBook-Pro:ruby dladner$ ls FILE* | wc -l
100
Danilas-MacBook-Pro:ruby dladner$ ls FILE{1..9}.txt
FILE1.txt FILE2.txt FILE3.txt FILE4.txt FILE5.txt FILE6.txt FILE7.txt FILE8.txt FILE9.txt
TL;DR COMMAND TO USE:
awk 'NR%100==1{fn && close(fn);fn="FILE"++i".txt";}{print > fn}' sample.txt
Try this, it will create different file each containing 200 lines:
count=0
filename=0
while [ $count -lt 9000 ]
do
count=$(($count + 200))
filename=$(($filename + 1))
head -$count abc.txt | tail -200 > pqr${filename}.txt
done