我想写一个shell脚本,它将从标准输入读取文件,删除所有字符串和空行字符,并将输出写入标准输出。该文件如下所示:
#some lines that do not contain <html> in here
<html>a<html>
<tr><html>b</html></tr>
#some lines that do not contain <html> in here
<html>c</html>因此,输出文件应该包含:
#some lines that do not contain <html> in here
a
<tr>b</html></tr>
#some lines that do not contain <html> in here
c</html>我试着写这个shell脚本:
read INPUT #read file from std input
tr -d '[:blank:]'
grep "<html>" | sed -r 's/<html>//g'
echo $INPUT然而,这个脚本根本不起作用。有什么想法吗?thx
发布于 2013-03-20 04:03:10
纯bash:
#!/bin/bash
while read line
do
#ignore comments
[[ "$line" = "\#" ]] && continue
#ignore empty lines
[[ $line =~ ^$ ]] && continue
echo ${line//\<html\>/}
done < $1输出:
$ ./replace.sh input
#some lines that do not contain in here
a
<tr>b</html></tr>
#some lines that do not contain in here
c</html>纯sed:
sed -e :a -e '/^[^#]/N; s/<html>//; ta' input | sed '/^$/d'发布于 2013-03-20 03:54:48
Awk可以轻松做到这一点:
awk '/./ {gsub("<html>","");print}' INPUTFILE首先,它对至少包含一个字符的每一行进行操作(因此空行将被丢弃),并将该行上的"<html>“全局替换为一个空字符串,然后打印出来。
https://stackoverflow.com/questions/15509058
复制相似问题