带有文字字符串的 sed——不是输入文件 [英] sed with literal string--not input file

查看:18
本文介绍了带有文字字符串的 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.

我试着用管道来回声:

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 期望文件作为参数

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

编辑:

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

if you use ksh or any other ones :

echo string | sed ...

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

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