How to read and Split a file?

Hi,

I have a .csv file which I want to split into smaller files as .csv format only

Thanks in advance.

There is a specific forum for 'emergency' or quick requests.

What have you tried?

1 Like

hi joyeg thanks for your reply
well i am new to scripting i require your assitance in splitting a file into multiple files.i request for your suggestion.. can you help me with the code

---------- Post updated at 08:51 AM ---------- Previous update was at 08:41 AM ----------

please help out guys thanks in advance

Start with (always) looking at the man pages.

man head
man tail
man split

By your luck, all the three man pages are very small (single page), and you can understand them very easily.
Please ask back if you have any confusion about understanding.

1 Like

thanks clx
but i need to read the file and then split it when i run automatically all the file format should be in csv

Show us your sample input and expected output.

1 Like

thanks

While we always encourage here to show the efforts and at least start trying something. This is the way to learn and also it will give us the feeling that we are not doing your work instead we are here to help you to get your work done.

Did you manage to look at the man pages I have posted earlier? Can you think the way you can use those commands to extract the first 100 lines and last 33 lines?

You can read the file with

while read eachline
do
 ## do your stuff with $eachline
done < your_file

You can apply your logic with incremental variable or something.
Think towards your solution.

1 Like

It could even be done without read with just the commands that clx suggested.. Is this homework?

yup head and tail command is working well for tis thank you

---------- Post updated at 05:43 AM ---------- Previous update was at 05:10 AM ----------

the head and tail for the firest and the last file is is working is uing the harcoding of the records but for the remaining it is not

For the remaining you can use split . Can you show us what you tried so far?

1 Like

yup

head -n 33 filename.csv > new1.csv
while  read line
split filename.csv 
done <filename.csv
tail -n 100 filename.csv

Hi, you cannot read and write to the same file at the same time like that.

Try something like:

{ n=0
  while IFS= read -r line && [ $(( n+=1 )) -le 100 ]
  do
    printf "%s\n" "$line"
  done
  split -l1000 - outfile
} < infile > outfilea0

--
It seems a while read loop was used after all :slight_smile:

1 Like

it works using for loop as well
can u suggest me to split using the comand line arguments

Do you mean what are the command line arguments to split? Have a look at man split .

1 Like

NOPE
i meant my myfiles must be split using command lind line arguments
eg: ./a.sh 2 1000 5

You never answered Scrutinizer's question: Is this homework?

What shell are you using?

PS How do the command line arguments in ./a.sh 2 1000 5 relate to your original request to put 100 lines in one file and split the remainder in 1000 line chunks?

1 Like

nope tis isnt i am trying to split my files as part of my own implemantation don i just gave an example

i am trying to modify my code

And, how do the command line arguments in ./a.sh 2 1000 5 relate to your original request to put 100 lines in one file and split the remaining lines in 1000 line chunks?

1 Like

First file having 100 records and last file having 33 and rest will have 1000 records each