[英]sed replace entire line at line number with a string that contains nested quotes
I want to use sed
to replace a certain line with my own string that has to contain some nested quotes because it will be read by another programme and itself executing commands. 我想用sed
用我自己的字符串替换某些行,该字符串必须包含一些嵌套的引号,因为它将被另一个程序及其本身执行的命令读取。
sed -i '63s/^.*$/mystring/' file.txt
Where 63
is the line number, ^.*$
is selecting the entire line to replace, mystring
is the replacement string and file.txt
is the file to work on. 其中63
是行号, ^.*$
选择要替换的整行, mystring
是替换字符串, file.txt
是要处理的文件。
Say for example I want to make mystring
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
how am I supposed to go about that? I've tried escaping the quotes. 比如说我想让mystring
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
mystring
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
我应该怎么做呢?我已经尝试了转义引号。
You can for example do the following: 例如,您可以执行以下操作:
sed -i.bak "63s|^.*$|$string|" file.txt
A wayis to fetch it from a file. 一种方法是从文件中获取它。 Also, note double quotes in sed
are necessary to have the variable expanded. 另外,请注意,必须使用sed
中的双引号来扩展变量。 Finally, use another delimiter for sed
and store $string
in a variable. 最后,对sed
使用另一个定界符,并将$string
存储在变量中。 For example, |
例如, |
. 。 This is because your $string
contain slashes and make sed
see a command like sed "s/^.*$/something/blabla/"
, which is so many slashes. 这是因为您的$string
包含斜杠,并使sed
看到诸如sed "s/^.*$/something/blabla/"
类的命令,它是如此之多。
Also, -i.bak
creates a backup of file.txt
--> file.txt.bak
. 同样, -i.bak
创建file.txt
> file.txt.bak
的备份。
$ cat a
hel
lo
ooo
bye
$ cat b
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
$ string=$(cat b)
$ sed "3s|^.*$|$string|" a
hel
lo
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
bye
If you don't want to do variable assignment, then just escape the problematic characters in your replacement string. 如果您不想进行变量分配,则只需在替换字符串中转义有问题的字符即可。 In this case, just double quotes "
: 在这种情况下,只需在双引号"
:
sed -i.bak "63s|^.*$|exec: echo 'echo \"127.0.0.1 localhost\" >> /etc/hosts\"'|" file.txt
^ ^ ^
$ sed "3s|^.*$|exec: echo 'echo \"127.0.0.1 localhost\" >> /etc/hosts\"'|" a
hel
lo
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
bye
This might work for you (GNU sed & Bash): 这可能适合您(GNU sed和Bash):
cat <<\! >quoted-text.txt
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
!
sed $'63{r quoted-text.txt\nd}' file
Put the difficult line of text into a file and replace the required line by reading said file and deleting the original line. 将困难的文本行放入文件中,并通过读取该文件并删除原始行来替换所需的行。
You could try this also, 您也可以尝试
sed -i "63s~.*~exec: echo \'echo \"127.0.0.1 localhost\" >> /etc/hosts\"\'~g" file
It just need escaping of quotes and a different sed delimiter. 它只需要转义引号和不同的sed分隔符。
*Example: *例:
$ sed "2s~.*~exec: echo \'echo \"127.0.0.1 localhost\" >> /etc/hosts\"\'~g" file
abc, xym krg plf, 763, bla
exec: echo 'echo "127.0.0.1 localhost" >> /etc/hosts"'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.