Replace every n'th occurrence in huge line in a loop

后端 未结 4 1305
眼角桃花
眼角桃花 2020-12-11 09:55

I have this line for example:

1,2,3,4,5,6,7,8,9,10

I want to insert a newline (\\n) every 2nd occurrence of \",\" (replace the 2nd, with ne

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 10:00

    Can't add comment to wintermutes answer but it doesn't need the first , section as it will have to have had a previous field to be comma separated.

    sed 's/\(,[^,]*\),/\1\n/g'
    

    Will work the same

    Also I'll add another alternative( albeit worse and leaves a trailing newline)

    echo "1,2,3,4,5,6,7,8,9,10" | xargs -d"," -n2 | tr ' ' ','
    

提交回复
热议问题