파일이 첫 번째 열 이름으로 분할됨

파일이 첫 번째 열 이름으로 분할됨

다음 CSV 파일이 있습니다.

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

한 줄씩 분할하여 첫 번째 열의 이름으로 파일에 저장하고 싶습니다.

예:

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

나는 다음 명령을 사용합니다.

 $ split -l 1 file.txt new  

그러나 이것은 newa, newb, newc, newd 등을 생성합니다! ?

답변1

반면

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

그 다음에

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

밝혀지다

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

==> 21.csv <==
{"21;"Example2";"300";"Some here...";}

답변2

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

substr파일 이름을 가져와서 추가 .csv하고 NR>1헤더를 무시하고 마지막으로 파일에 쓰는 데 사용됩니다 .

$ head *csv

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

==> 21.csv <==
"21;"Example2";"300";"Some here...";

관련 정보