sed with literal string--not input file

前端 未结 3 779
-上瘾入骨i
-上瘾入骨i 2020-12-12 23:09

This should be easy: I want to run sed against a literal string, not an input file. If you wonder why, it is to, for example edit values stored in variabl

相关标签:
3条回答
  • 2020-12-12 23:41

    Works like you want:

    echo "A,B,C" | sed s/,/\',\'/g
    
    0 讨论(0)
  • 2020-12-12 23:41

    My version using variables in a bash script:

    Find any backslashes and replace with forward slashes:

    input="This has a backslash \\"
    
    output=$(echo "$input" | sed 's,\\,/,g')
    
    echo "$output"
    
    0 讨论(0)
  • 2020-12-13 00:01

    You have a single quotes conflict, so use:

     echo "A,B,C" | sed "s/,/','/g"
    

    If using bash, you can do too (<<< is a here-string):

    sed "s/,/','/g" <<< "A,B,C"
    

    but not

    sed "s/,/','/g"  "A,B,C"
    

    because sed expect file(s) as argument(s)

    EDIT:

    if you use ksh or any other ones :

    echo string | sed ...
    
    0 讨论(0)
提交回复
热议问题