Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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

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
Bash sed搜索和替换不适用于html标记_Bash_Shell_Search_Sed_Replace - Fatal编程技术网

Bash sed搜索和替换不适用于html标记

Bash sed搜索和替换不适用于html标记,bash,shell,search,sed,replace,Bash,Shell,Search,Sed,Replace,我有两个问题: 1) 我尝试替换一对html标记,并在它们之间添加值: 我试图在这个skype上替换这个gmail 当我尝试这样做时: sed-i-e“s/gmail/skype/g”test.txt ,我收到错误: sed:-e表达式#1,字符18:未知的s选项` 我理解,我应该使用字符串,好的,让我们使用这个模式: sed-i-e“s/”gmail“/”skype“/g”test3.txt 在这种情况下,我还有其他错误: -su:td:没有这样的文件或目录 哪里有错误,请帮忙 2) 我只想在

我有两个问题:

1) 我尝试替换一对html标记,并在它们之间添加值: 我试图在这个
skype上替换这个
gmail

当我尝试这样做时:
sed-i-e“s/gmail/skype/g”test.txt

,我收到错误:

sed:-e表达式#1,字符18:未知的
s选项`

我理解,我应该使用字符串,好的,让我们使用这个模式:

sed-i-e“s/”gmail“/”skype“/g”test3.txt

在这种情况下,我还有其他错误:

-su:td:没有这样的文件或目录

哪里有错误,请帮忙

2) 我只想在专线上换货。例如,我有:

<tr>
    <td>click</td>
    <td>gmail</td>
    <td>message-type</td>
</tr>

点击
gmail
消息类型
我只想换第三行。如何执行此操作?

Ad 1)斜杠是sed s命令的特殊字符。这就是您的命令的解释方式:

s/<td>gmail</td>/<td>skype</td>/g
  ^        ^ ^ ^ ^
  |        | | | +- options start here
  |        | | +- replacement string stops here
  |        | +- replacement string starts here
  |        +- search pattern stops here 
  +- search pattern starts here
s/gmail/skype/g
^        ^ ^ ^ ^
|| | |+-选项从这里开始
|| |+-替换字符串在此停止
||+-替换字符串从这里开始
|+-搜索模式在此停止
+-搜索模式从这里开始
如果希望它表现为普通字符,请使用反斜杠引用它。如中所述

广告2)
sed-e'3s/…/”


PS.
man sed
是您的朋友。

将行号3与另一个分隔符组合,如(这样您就不需要反斜杠):

sed-i-e'3s#gmail#skype#g'test.txt

使用XML/HTML解析器(xmlstarlet、xmllint…)。了解引用(使用
)和转义(使用
\
)特殊字符(例如
/
)。要做到这一点,请查找sed教程并完成它。在学习SED(这是我最喜欢的一种大脑折磨)之前,应该考虑学习Perl或AWK。在做那件事之前。。见@Cyrus的评论。
sed-i-e“s@gmail@skype@g“test.txt
sed-e'3 s/../”
是错误的,因为这将替换文件的第三行,而不是第三行
td
。让我引用原始问题:我只想替换第三行。
sed -i -e '3 s#<td>gmail</td>#<td>skype</td>#g' test.txt