shell - How to treat a file use awk -


i want read file using awk got stuck on fourth field automatically breaks after comma.

data:- test.txt

"a","b","ls","this,is,the,test" "k","o","mv","this,is,the,2nd test" "c","j","cd","this,is,the,3rd test"  cat test.txt | awk -f , '{ ofs="|" ;print $2 $3 $4 }' 

output

"b"|"ls"|"this "o"|"mv"|"this "j"|"cd"|"this 

but output should this

"b"|"ls"|"this,is,the,test" "o"|"mv"|"this,is,the,2nd test" "j"|"cd"|"this,is,the,3rd test" 

any idea

use gnu awk fpat:

$ awk -v fpat='([^,]+)|(\"[^\"]+\")' -v ofs='|' '{print $2,$3,$4}' file "b"|"ls"|"this,is,the,test" "o"|"mv"|"this,is,the,2nd test" "j"|"cd"|"this,is,the,3rd test" 

see http://www.gnu.org/software/gawk/manual/gawk.html#splitting-by-content

with other awks you'd do:

$ cat tst.awk begin { ofs="|" } {     nf=0     delete f     while ( match($0,/([^,]+)|(\"[^\"]+\")/) ) {         f[++nf] = substr($0,rstart,rlength)         $0 = substr($0,rstart+rlength)     }     print f[2], f[3], f[4] }  $ awk -f tst.awk file "b"|"ls"|"this,is,the,test" "o"|"mv"|"this,is,the,2nd test" "j"|"cd"|"this,is,the,3rd test" 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -