perl - replace every nth (and multiples) occurrences of a character with another character

后端 未结 6 1019
余生分开走
余生分开走 2021-01-20 19:27

Does anyone know any unix commands/perl script that would insert a specific character (that can be entered as either hex (ie 7C) or as the actual character (ie |)) in the po

6条回答
  •  我在风中等你
    2021-01-20 20:09

    How about a nice, simple awk one-liner?

    awk -v RS=, '{ORS=(++i%3?",":"|");print}' file.csv
    

    One minor bug just occurred to me: it will print a , or | as the very last character. To avoid this, we need to alter it slightly:

    awk -v RS=, '{ORS=(++i%3?",":"|");print}END{print ""}' file.csv | sed '$d'
    

提交回复
热议问题