csv - awk and substitute semicolon (;) in appointed field -


can explain me small script.

echo -e "\"aa;bb\";cc ;\"dd ;ee\";  ff" | awk -v rs=\" -v ors=\" 'nr%2==0{gsub(";",",")} {print}' 

in script fields separated (;), if there 1 or more (;) inside field field surrounded "".it's csv-file.

therefore necessary replace (;) in fields further parsing.

the echo prints 2 lines:

"aa;bb";cc ;"dd ;ee";  ff 

and splits records each double quote, , in ones replace semicolons commas (gsub).

so, first record content before first double quote, it's blank record important part condition nr%2==0. nr 1 condition false, gsub() not executed, printed ors output double quote.

for second record content aa;bb, nr%2==0 true , replace semicolon.

for third record content ;cc ;, nr%2==0 false , printed.

and on until end of file.


Comments