0

I have this CSV file:

"Product ID";"Product Name";"Price";"Description";
"11;"Example";"200";"Descripcion here...";
"21;"Example2";"300";"Some here...";

I want to split it line by line and save them into the file with the name of first column.

Example:

11.csv {"11;"Example";"200";"Descripcion here...";}
21.csv {"21;"Example2";"300";"Some here...";}

I use this command:

 $ split -l 1 file.txt new  

But this create newa ,newb,newc,newd, etc!?

1

Given

$ cat > file.csv
"Product ID";"Product Name";"Price";"Description";
"11;"Example";"200";"Descripcion here...";
"21;"Example2";"300";"Some here...";

then

$ awk -F';' 'NR>1 {print "{" $0 "}" > substr($1,2) ".csv"}' file.csv

results in

$ head ??.csv
==> 11.csv <==
{"11;"Example";"200";"Descripcion here...";}

==> 21.csv <==
{"21;"Example2";"300";"Some here...";}
  • When i use this cmd in the original csv giveme errors I add the csv file for test csv file – Jess Jul 16 '18 at 2:46
  • Well that differs in several ways from what you posted in your question - including DOS-style BOM and line endings, and different quoting of the fields. – steeldriver Jul 16 '18 at 2:55
0
$ awk -F';' '{file=substr($1,2)".csv";if(NR>1){print $0 > file}}' inputFile

Use substr to get file name and append .csv to it, and NR>1 to ignore header, and finally write to file.

$ head *csv

==> 11.csv <==
"11;"Example";"200";"Descripcion here...";

==> 21.csv <==
"21;"Example2";"300";"Some here...";
  • When i use this cmd in the original csv giveme errors I add the csv file for test [csv file(petshopanimall.com/1.csv) – Jess Jul 16 '18 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.