用文字字符串sed-不输入文件 [英] sed with literal string--not input file

查看:149
本文介绍了用文字字符串sed-不输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易:我想对文字字符串而不是输入文件运行 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.

我尝试通过管道将其回显:

I tried piping it to echo:

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

我得到提示.

正确的方法是什么?

推荐答案

您有单引号冲突,因此请使用:

You have a single quotes conflict, so use:

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

如果使用的问题,您也可以这样做(<<<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期望文件作为参数

编辑:

如果您使用或其他任何问题:

if you use ksh or any other ones :

echo string | sed ...

这篇关于用文字字符串sed-不输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆