如何使用 grep/sed 打印文件,不包括注释和空行? [英] How to print a file, excluding comments and blank lines, using grep/sed?

查看:55
本文介绍了如何使用 grep/sed 打印文件,不包括注释和空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个包含一系列注释的文件,例如:

I'd like to print out a file containing a series of comments like:

    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
    #   SSL Engine Switch:

本质上,文件包含多个缩进级别,其中注释以 # 符号开头.

In essence, the file contains multiple indentation levels, where a comment starts with a # symbol.

grep 应该删除空行,以及文本前有哈希符号的行(暗示这些是注释).

grep should remove blank lines, and also lines where there is a hash symbol before text (implying that these are comments).

我知道可以通过以下方式删除空行:grep -v '^$'

I know that blank lines can be deleted via: grep -v '^$'

但是,如何删除带有前导空格的行,然后是 # 符号,并仅打印出带有实际代码的行?我想在 bash 中使用 grep 和/或 sed 执行此操作.

However how can I delete lines with leading whitespace, and then a # symbol, and print out only lines with actual code? I would like to do this in bash, using grep and/or sed.

推荐答案

With grep:

grep -v '^\s*$\|^\s*\#' temp

在 OSX/BSD 系统上:

On OSX / BSD systems:

grep -Ev '^\s*$|^\s*\#' temp

这篇关于如何使用 grep/sed 打印文件,不包括注释和空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆