编写shell脚本

编写shell脚本,shell,Shell,我想编写一个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>

我想编写一个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>
#此处不包含的某些行
A.
B
#此处不包含的某些行
C
因此,输出文件应包含:

#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>
#此处不包含的某些行
A.
B
#此处不包含的某些行
C
我尝试编写以下shell脚本:

read INPUT #read file from std input
tr -d '[:blank:]'
grep "<html>" | sed -r 's/<html>//g'
echo $INPUT
读取输入#从std输入读取文件
tr-d'[:空白:'
grep“| sed-r's///g”
echo$输入
然而,这个脚本根本不起作用。有什么想法吗?thx

Awk可以轻松做到:

awk '/./ {gsub("<html>","");print}' INPUTFILE
awk'/./{gsub(“,”);print}输入文件
首先,它对每一行至少使用一个字符进行操作(因此空行被丢弃),并用行上的空字符串全局替换“
”,然后打印它。

Pure bash:

#!/bin/bash

while read line
do
    #ignore comments
    [[ "$line" = "\#" ]] && continue
    #ignore empty lines
    [[ $line =~ ^$ ]] && continue
    echo ${line//\<html\>/}
done < $1
#/bin/bash
读行时
做
#忽略评论
[[“$line”=“\\\”]&继续(&C)
#忽略空行
[$line=~^$]&继续(&C)
echo${line/\/}
已完成<$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>
$。/replace.sh输入
#此处不包含的某些行
A.
B
#此处不包含的某些行
C
纯sed:

sed -e :a -e '/^[^#]/N; s/<html>//; ta' input | sed '/^$/d'
sed-e:a-e'/^[^#]/N;s//;ta'输入| sed'/^$/d'

如果可能的话,您可能希望在Perl(或某种shell以外的其他语言)中尝试这一点:@summea我不能。我得用#/usr/bin/bash应该保留注释吗?我想我不明白为什么一个文档中也有多个
对……我也不知道。这只是我的老师给usOP的一些随机文件需要保留注释,我只能使用grep和sed。但是//意味着什么?它是指当前目录吗?@HannaGabby-
/./
是一个正则表达式,表示[[“$line”=“\\\\”]”所指的一个字符[任意]?我不能只在上面的源代码中使用grep和sedsee注释,所以第一个sed将被删除,但是第二个sed做什么呢?第二个sed删除空行