String manipulation

Hi ,

I am getting a string like

aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf,rtyrtyr,45654654,ddfdfdfgdfg,dfgdfgdg..........

.

Now what I need is to format it.
So after each nth comma I need one newline. So the above will look like

when n=3

aaa,bbb,sdsdad,
sdfsdf,sdfsdfdsf,rtyrtyr,
45654654,ddfdfdfgdfg,dfgdfgdg...........

Please help.

Try like...

echo "aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf"| sed 's/,/&\n/3' 

Working .. Thank you.

Does it? Try with longer strings... and try this:

echo "aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf,wer,345,dh,zhrhzr6,rtz,zr"| sed -r 's/([^,]*,){3}/&\n/g'

If your sed does not accept the -r option, try this:

 echo "aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf,wer,345,dh,zhrhzr6,rtz,zr"| sed 's/[^,]*,[^,]*,[^,]*,/&\n/g'
1 Like

I know what you mean :slight_smile:

I tried with longer string 1st time... then got the issue... posted not working .. then I got work around and edited that to working :stuck_out_tongue:

Your one working like a charm .. even better than my work around. Thank you