Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换shell中的特殊字符?_Shell - Fatal编程技术网

如何替换shell中的特殊字符?

如何替换shell中的特殊字符?,shell,Shell,我有一个字符串: sam1/0/2 我必须使用shell命令替换它,以便: sam-1/0/2/4:4 有什么帮助吗?谢谢在替换为sed时,您需要转义这些特殊字符 请参阅下面的命令 sed "s/sam1\/0\/2/sam-1\/0\/2\/4\:4/g" ---- g is for all occurrences on a line sed "s/sam1\/0\/2/sam-1\/0\/2\/4\:4/" ---- first occurrence on a line 例如 [ro

我有一个字符串:

sam1/0/2
我必须使用shell命令替换它,以便:

sam-1/0/2/4:4

有什么帮助吗?谢谢

在替换为sed时,您需要转义这些特殊字符

请参阅下面的命令

sed "s/sam1\/0\/2/sam-1\/0\/2\/4\:4/g" ---- g is for all occurrences on a line
sed "s/sam1\/0\/2/sam-1\/0\/2\/4\:4/"  ---- first occurrence on a line
例如

[root@giam39 ~]# sed "s/sam1\/0\/2/sam-1\/0\/2\/4\:4/" file1.txt > file2.txt
[root@giam39 ~]# cat file1.txt
sam1/0/2
[root@giam39 ~]# cat file2.txt
sam-1/0/2/4:4
[root@giam39 ~]#
sed
-用于过滤和转换文本的流编辑器

使用管道(
|
)可以将
$string
的输出传递到
sed
。 通过使用sed,您可以在具有特定模式的字符串中找到子字符串

结构:
sed-e's/ught/beauty/g'

string="sam1/0/2"
echo "$string" | sed -e 's/1\/0\/2/-1\/0\/2\/4:4/g'