使用我的shell脚本,当我运行时./Test1 hello result
。
应该将其hello
作为标准输入,并将结果作为标准输出。外壳应该之前删除任何空白<td>
,</td>
和abc
所以,我这样写脚本
tr -d [:blank:] < $1
grep -r "<td>" $1 | sed -r 's/<td>//g' > $2
sed -r 's/<\/td>//g' $2
sed -r 's/abc//g' $2
但是,当我运行此命令时,结果文件的内容与hello文件的内容完全相同(唯一的区别是删除了空格)
文件问候内容:
<td>hello</td>
<td>hello</td>
<td>hello</td>
<td>abc</td>
<td>abc</td>
我如何sed
将更改应用于目标文件?
如果要将更改从sed
回存储到文件,请使用以下-i
选项:
$ cat file
<head>abc</head>
<td>hello</td>
<td>hello</td>
<td>hello</td>
<td>abc</td>
<td>abc</td>
<h1>abc</h1>
$ sed -ni '/<td>/{s/^\s*//;s/abc//;s/<\/\?td>//g;p}' file
$ cat file
hello
hello
hello
编辑:如果我们使用不同的分隔符sed
并使用扩展的regexp选项,则regexp更清晰-r
:
$ sed -r 's_</?td>__g' file
hello
hello
hello
abc
abc
在?
前面的字符可选的,所以化妆/
不必存在使得正则表达式匹配<td>
,并</td>
于一体。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句