当您使用以下命令在标准输出中显示源代码或配置文件的内容时 cat
命令,您将看到该文件中的所有内容,包括注释行和空行,对吗? 是的。 如果您只想查看未注释的行而忽略所有其他行怎么办? 好吧,可以忽略文件输出中的注释行和空行。 在这个简短的指南中,我们将看到如何在 Linux 中打印没有注释和空行的文件。 当您想快速查看包含大量注释和空行的配置文件时,这会很有用。
什么是计算机编程中的注释?
如果您查看源代码或配置文件,您会注意到很多行以星号开头 "*"
或散列 "#"
或斜线 "/"
或分号 ";"
. 这些线被称为 评论. 在计算机编程中,注释是人类可读的描述或注释,用于阐明代码的目的。 它们帮助用户和其他程序员轻松理解代码在做什么。 通常,编译器和解释器会忽略注释和空行。 它们仅适用于程序员。 注释的语法因不同的编程语言而异。
现在让我们看看如何排除或跳过这些注释和空行,只显示没有注释的行。
在 Linux 中打印没有注释和空行的文件
让我给你看一下内容 sources.list
我的 Ubuntu 系统文件:
$ cat /etc/apt/sources.list
示例输出:
# deb cdrom:[Ubuntu 18.04.2 LTS _Bionic Beaver_ - Release amd64 (20190210)]/ bionic main restricted # See https://help.ubuntu.com/community/UpgradeNotes for how to upgrade to # newer versions of the distribution. deb https://it-mirrors.evowise.com/ubuntu/ focal main restricted # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic main restricted ## Major bug fix updates produced after the final release of the ## distribution. deb https://it-mirrors.evowise.com/ubuntu/ focal-updates main restricted # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic-updates main restricted ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team. Also, please note that software in universe WILL NOT receive any ## review or updates from the Ubuntu security team. deb https://it-mirrors.evowise.com/ubuntu/ focal universe # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic universe deb https://it-mirrors.evowise.com/ubuntu/ focal-updates universe # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic-updates universe ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team, and may not be under a free licence. Please satisfy yourself as to ## your rights to use the software. Also, please note that software in ## multiverse WILL NOT receive any review or updates from the Ubuntu ## security team. deb https://it-mirrors.evowise.com/ubuntu/ focal multiverse # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic multiverse deb https://it-mirrors.evowise.com/ubuntu/ focal-updates multiverse # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic-updates multiverse ## N.B. software from this repository may not have been tested as ## extensively as that contained in the main release, although it includes ## newer versions of some applications which may provide useful features. ## Also, please note that software in backports WILL NOT receive any review ## or updates from the Ubuntu security team. deb https://it-mirrors.evowise.com/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://in.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse ## Uncomment the following two lines to add software from Canonical's ## 'partner' repository. ## This software is not part of Ubuntu, but is offered by Canonical and the ## respective vendors as a service to Ubuntu users. # deb https://archive.canonical.com/ubuntu bionic partner # deb-src https://archive.canonical.com/ubuntu bionic partner deb https://it-mirrors.evowise.com/ubuntu/ focal-security main restricted # deb-src https://security.ubuntu.com/ubuntu bionic-security main restricted deb https://it-mirrors.evowise.com/ubuntu/ focal-security universe # deb-src https://security.ubuntu.com/ubuntu bionic-security universe deb https://it-mirrors.evowise.com/ubuntu/ focal-security multiverse # deb-src https://security.ubuntu.com/ubuntu bionic-security multiverse
你有没有看到很多行被“#”字符注释掉了? 它使文件的可读性降低。 这只是一个小文件,所以没什么大不了的。 但是当你阅读一个很长的配置文件时,例如“httpd.conf”或“php.ini”,你将不得不浏览大量的注释和空行,并且找出哪些行是活动的有点令人困惑哪些不是。
要过滤文件输出中显示的所有注释和空行,请使用 格雷普 命令如下:
$ grep "^[^#*/;]" /etc/apt/sources.list
这里,
- 首先
^
字符表示给定文件中行的开始,即/etc/apt/sources.list
. [^#*/;]
– 将显示所有以这四个字符以外的任何字符开头的行"#"
,"*"
,"/"
,";"
. 换句话说,所有以字符开头的行"#"
,"*"
,"/"
,";"
将被排除在输出之外。
上述命令的示例输出:
deb https://it-mirrors.evowise.com/ubuntu/ focal main restricted deb https://it-mirrors.evowise.com/ubuntu/ focal-updates main restricted deb https://it-mirrors.evowise.com/ubuntu/ focal universe deb https://it-mirrors.evowise.com/ubuntu/ focal-updates universe deb https://it-mirrors.evowise.com/ubuntu/ focal multiverse deb https://it-mirrors.evowise.com/ubuntu/ focal-updates multiverse deb https://it-mirrors.evowise.com/ubuntu/ focal-backports main restricted universe multiverse deb https://it-mirrors.evowise.com/ubuntu/ focal-security main restricted deb https://it-mirrors.evowise.com/ubuntu/ focal-security universe deb https://it-mirrors.evowise.com/ubuntu/ focal-security multiverse
看? 所有评论和空行都消失了。 现在输出是非常可读的。
看看下面的截图:
在左侧,您会看到带有注释和空行的文件内容。 在右侧,所有注释和空行都被忽略 "grep"
命令。
我更喜欢 grep 方式来过滤输出中显示的不必要的行。 您也可以使用 awk
和 sed
命令也是如此。
打印不包括所有注释和空行的文件内容 "awk"
命令,运行:
$ awk '$1 ~ /^[^;#]/' /etc/apt/sources.list
示例输出:
显示没有注释的行开始使用 "sed"
命令,运行:
$ sed -e '/^#/d' /etc/apt/sources.list
有关更多使用详细信息,请参阅相应命令的手册页。
$ man awk
$ man grep
$ man sed
希望这可以帮助。
awk 命令grep 命令LinuxLinux 基础Linux 命令Linux howtoLinux tipsSed 命令