我想编写一个 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
但是这个脚本根本不起作用。任何想法?谢谢
最佳答案
纯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'
关于shell - 编写 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509058/