这应该很容易:我想针对文字字符串而不是输入文件运行 sed。如果您想知道为什么,例如编辑存储在变量中的值,不一定是文本数据。
当我做:
sed 's/,/','/g' "A,B,C"
其中 A,B,C 是我要更改为 A','B','C 的文字
我明白了
Can't open A,B,C
好像它认为 A,B,C 是一个文件。
我尝试通过管道将其回显:
echo "A,B,C" | sed 's/,/','/g'
我得到提示。
正确的方法是什么?
你有一个单引号冲突,所以使用:
echo "A,B,C" | sed "s/,/','/g"
如果使用 bash,您也可以这样做(<<< 是 here-string):
<<<
here-string
sed "s/,/','/g" <<< "A,B,C"
但不是
sed "s/,/','/g" "A,B,C"
因为 sed 期望文件作为参数
sed
编辑:
如果您使用 ksh 或任何其他:
echo string | sed ...
像你想要的那样工作:
echo "A,B,C" | sed s/,/\',\'/g
我在 bash 脚本中使用变量的版本:
查找任何反斜杠并用正斜杠替换:
input="This has a backslash \\" output=$(echo "$input" | sed 's,\\,/,g') echo "$output"
不定期副业成功案例分享