hi,
i've a file with few rows that ends with a comma(,). I need to fetch just those lines and merge them in to a single line and remove the last comma
Thanks
hi,
i've a file with few rows that ends with a comma(,). I need to fetch just those lines and merge them in to a single line and remove the last comma
Thanks
Try this
awk '{if($0~/\,$/){a=a$0}else{print $0}}END {print substr(a,0,length(a)-1)}' file.txt
your command will fail if the text file has more comma separated blocks. i.e:
cde
aaa
client_list,
company_list,
policy_list,
scheme_list,
blahblah
1,
2,
3,
4,
a
b
c
this statement should work:
awk '{if ($0~/\,$/) {a=a$0} else {if(a)print substr(a,0,length(a)-1);a="";print $0}}' yourFile.txt
This was the statement, and it is mentioned that "it has to be a single line" combining all lines ending with comma.
awk '/\,$/{a=a$0;next}{sub("\,$",z,a);print (a?a RS:z)$0;a=z}' file.txt
---------- Post updated at 10:46 AM ---------- Previous update was at 10:13 AM ----------
Depending on your needs
awk '/\,$/{a=a$0;next}END{sub("\,$",z,a);print a}' file.txt