简体   繁体   English

如何在shell脚本中转​​义冒号和双斜杠?

[英]How to escape colon and double slash in shell script?

I am writing shell script to replace a line a file which has an URI.I am getting error in using URI. 我正在编写shell脚本来替换具有URI的文件行。我在使用URI时遇到错误。

Line to be placed: 要放置的行:

   source 'http://rubygems.org'

My script: 我的剧本:

   #sed -i '1s/.*/  source 'https://rubygems.org' /' /Gemfile

It is not able to identify single quote. 它无法识别单引号。

Error i am getting: "sed: -e expression #1, char 23: unknown option to `s'" 我得到的错误: “sed:-e expression#1,char 23:`s'的未知选项”

You can use alternate delimeters in your sed command. 您可以在sed命令中使用备用分隔符。

For example, years ago I had a sed line that had to swap URLs and I used the @ at sign. 例如,几年前我有一个必须交换URL的sed行,我使用了@ at符号。

For an example, create a text file 'file.txt' with the following: I have a unix operating system from California 例如,使用以下内容创建文本文件“file.txt”:我有一个来自加利福尼亚州的unix操作系统

Run the following in your shell, using the @ instead of the usual slash delimiter. 使用@而不是通常的斜杠分隔符在shell中运行以下命令。

$ sed 's@unix@linux@' file.txt
I have a linux operating system from California

The simplest solution is to use something like this instead. 最简单的解决方案是使用类似的东西。

sed -i "1s,.*,  source 'https://rubygems.org' ," /Gemfile

Double quotes around the sed script to allow internal single quotes and s,,, instead of s/// (since you can use just about any single character for the delimiter that you want). sed脚本周围的双引号允许内部单引号和s,,,而不是s/// (因为您可以使用任何单个字符作为所需的分隔符)。

sed accepts different separators: sed接受不同的分隔符:

sed -i '1s|.*|  source "https://rubygems.org" |' /Gemfile

And within single quotes you can also use double quotes. 在单引号内,您还可以使用双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM