0

I have scenario where i want to split on every 5 records into new file Not getting how to start with this approach where i need to create split file dynamically

For example below is data in file :

Which contain 11 records , The script should split it into 3 different set : like below

AA
BB
CC
DD
EE
FF
GG
HH
JK
LLL
HJ

Expected Output :

abc1.txt -> first 5 records 
abc2.txt -> next 5 records 
abc3.txt -> last 1 records [ as it as only one records after splitting : count of file is odd] 

My Script :

file_count=`awk 'END{print FNR}' demo.txt` 
a=1

#Iterate the loop until a less than 10
while [ $a -lt $file_count ]
do
    # Print the values
    echo $a
    
    if [ $a -eq 5 ]
    then
         
    fi 

    # increment the value
    a=`expr $a + 1`

done < /d/demo.txt

Any easy approach to do with unix or python

codeholic24
  • 955
  • 4
  • 22
  • 2
    There is `split` in unix/linux - `split -l5 file abc` – andr3yk Jun 04 '21 at 11:58
  • @andr3yk : Thanks it work like charm .. but every file as last line empty how to get rid of it – codeholic24 Jun 04 '21 at 15:55
  • I guess it's your implementation detail, but ask if you really want to remove it? A good read: https://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file. If you still want to, use `sed` or `truncate` as per: https://stackoverflow.com/questions/27305177/how-can-i-remove-the-last-character-of-a-file-in-unix – andr3yk Jun 06 '21 at 11:22

0 Answers0