How to replace one character with two characters using tr

前端 未结 5 1536
栀梦
栀梦 2020-12-17 08:10

Can tr replace one character with two characters?

I am trying to replace \"~\" with \"~\\n\" but the output does not produce the newline.

$ echo \"a         


        
5条回答
  •  感动是毒
    2020-12-17 08:16

    no can do, sorry.

    tr is meant to transliterate one character with another.

    there are numerous options, but I would use awk, i.e.

    echo "asdlksad ~ adlkajsd ~ 12345" | awk '{gsub(/[~]/, "&\n")};1'
    

    output

    asdlksad ~
     adlkajsd ~
     12345
    

提交回复
热议问题