带有文字字符串的 sed——不是输入文件 [英] sed with literal string--not input file
问题描述
这应该很简单:我想针对文字字符串而不是输入文件运行 sed.如果您想知道为什么,例如编辑存储在变量中的值,不一定是文本数据.
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 variables, not necessarily text data.
当我这样做时:
sed 's/,/','/g' "A,B,C"
其中 A,B,C 是我想更改为 A','B','C 的文字
where A,B,C is the literal which I want to change to A','B','C
我明白了
Can't open A,B,C
好像它认为 A,B,C 是一个文件.
As though it thinks A,B,C is a file.
我试着用管道来回声:
echo "A,B,C" | sed 's/,/','/g'
我收到提示.
正确的做法是什么?
推荐答案
你有单引号冲突,所以使用:
You have a single quotes conflict, so use:
echo "A,B,C" | sed "s/,/','/g"
如果使用 bash,你也可以这样做(<<<
是一个 here-string
):
If using bash, you can do too (<<<
is a here-string
):
sed "s/,/','/g" <<< "A,B,C"
但不是
sed "s/,/','/g" "A,B,C"
因为 sed
期望文件作为参数
because sed
expect file(s) as argument(s)
编辑:
如果您使用 ksh 或任何其他问题:
if you use ksh or any other ones :
echo string | sed ...
这篇关于带有文字字符串的 sed——不是输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!