O'Reilly logo

Bash. Receptury by Cameron Newham, JP Vossen, Carl Albing

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.18. Zapis pliku danych w formacie CSV

Problem

Chcemy przekształcić plik z danymi w plik formatu CSV (w którym poszczególne wartości są rozdzielane za pomocą znaku przecinka).

Rozwiązanie

Aby przekształcić dane do formatu CSV, wystarczy wykorzystać polecenie awk:

$ awk 'BEGIN { FS="\t"; OFS="\",\"" } { gsub(/"/, "\"\""); $1 = $1; printf "\"%s\"\n",
$0}' rozdzielone_tabulatorami
"Wiersz 1","Pole 1","Pole 2","Pole 3","Pole 4","Pole 5 ze ""znakami"" cudzysłowu"
"Wiersz 2","Pole 1","Pole 2","Pole 3","Pole 4","Pole 5 ze ""znakami"" cudzysłowu"
"Wiersz 3","Pole 1","Pole 2","Pole 3","Pole 4","Pole 5 ze ""znakami"" cudzysłowu"
"Wiersz 4","Pole 1","Pole 2","Pole 3","Pole 4","Pole 5 ze ""znakami"" cudzysłowu"

To samo zadanie można wykonać za pomocą skryptu ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required