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>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/

相关文章:

bash - Unix 验证文件没有内容和空行

android - 如何使用 adb shell 启动我的 Android 应用程序?

linux - 在 BASHRC 与命令行中启动后台进程之间的区别

shell - 在 'grep' 结果中包含 header

linux - uniq 命令未检测到重复行

Ubuntu下的shell ps命令

django - 使用 docker/fig 运行 Django 开发服务器时,为什么隐藏了一些日志输出?

linux - Bash read -p 不起作用

linux - 谁阅读正则表达式、Shell 或命令?

bash - 将所有脚本参数复制到另一个变量