-1

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

2
  • Current wording suggests homework assignment material. Feb 11, 2014 at 15:07
  • It is very unclear what the end goal is here. Feb 11, 2014 at 22:54

3 Answers 3

5

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
1
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 
1

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

Not the answer you're looking for? Browse other questions tagged .