linux - Compare string and join value into one field in bash shell -
i'm writing script modify csv file
here's csv file:
"id", "subject" , "channels", "description" "24" , "bind-0.9.3" , "linux", "bind (berkeley net name domain) implementation of dns (domain name system) protocols" "24" , "bind-0.9.3", "fedora", "bind (berkeley net name domain) implementation of dns (domain name system) protocols" "25" , "tar-8.0.1" , "debian", "tar package" "25" , "tar-8.0.1", "ubuntu" , "tar package"
now, want compare "id" value . if same value, bring together "channels" 1 field
here's expected result:
"id", "subject" , "channels", "description" "24" , "bind-0.9.3" , "linux , fedora", "bind (berkeley net name domain) implementation of dns (domain name system) protocols" "25" , "tar-8.0.1" , "debian , ubuntu", "tar package"
does have thought using awk, sed or else in case ? much regards,
$ cat tst.awk begin { fs="[[:space:]]*,[[:space:]]*"; ofs=" , " } nr==1 { print; next } { subj[$1] = $2 desc[$1] = $4 if ($1 in chans) { chans[$1] = chans[$1] ofs $3 } else { chans[$1] = $3 cnt2chan[++numchans] = $1 } } end { (channr=1; channr<=numchans; channr++) { chan = cnt2chan[channr] gsub(/\"/,"",chans[chan]) print chan, subj[chan], "\"" chans[chan] "\"", desc[chan] } } $ $ awk -f tst.awk file "id", "subject" , "channels", "description" "24" , "bind-0.9.3" , "linux , fedora" , "bind (berkeley net name domain) implementation of dns (domain name system) protocols" "25" , "tar-8.0.1" , "debian , ubuntu" , "tar package"
linux bash awk sed
No comments:
Post a Comment