stringbashreplacestr-replacetr

Substring substitution in bash


my problem of today is to replace in a string like this --> 6427//6422 6429//6423 6428//6421

every // with a ,. I tried with different commands:

Someone has an idea of a way to do it?


Solution

  • You can use BASH string manipulations (need to escape / with \/):

    s='6427//6422 6429//6423 6428//6421'
    echo "${s//\/\//,}"
    6427,6422 6429,6423 6428,6421
    

    Similarly using awk:

    awk -F '//' -v OFS=, '{$1=$1}1' <<< "$s"
    6427,6422 6429,6423 6428,6421
    

    PS: tr cannot be used here since tr translates each character in input to another character in the output and here you're dealing with 2 characters //.