Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace end of line with comma and put parenthesis in sed/awk

I am trying to process the contents of a file from this format:

this1,EUR 
that2,USD
other3,GBP

to this format:

this1(EUR),that2(USD),other3(GBP)

The result should be a single line.

As of now I have come up with this circuit of commands that works fine:

cat myfile | sed -e 's/,/\(/g' | sed -e 's/$/\)/g' | tr '\n' , | awk '{print substr($0, 0, length($0)- 1)}'

Is there a simpler way to do the same by just an awk command?

like image 954
thanos.a Avatar asked Dec 14 '25 02:12

thanos.a


2 Answers

Another awk:

$ awk -F, '{ printf "%s%s(%s)", c, $1, $2; c = ","} END { print ""}' file

1(EUR),2(USD),3(GBP)

like image 156
jas Avatar answered Dec 16 '25 21:12

jas


Following awk may help you on same.

awk -F, '{val=val?val OFS $1"("$2")":$1"("$2")"} END{print val}' OFS=,  Input_file
like image 23
RavinderSingh13 Avatar answered Dec 16 '25 21:12

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!