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

后端 未结 4 1310
眼角桃花
眼角桃花 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:06

    If I understand what you're trying to do correctly, then

    echo '1,2,3,4,5,6,7,8,9,10' | sed 's/\([^,]*,[^,]*\),/\1\n/g'
    

    seems like the most straightforward way. \([^,]*,[^,]*\) will capture 1,2, 3,4, and so forth, and the commas between them are replaced with newlines through the usual s///g. This will print

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

提交回复
热议问题