我想查一下/etc/passwd
$ cat -n /etc/passwd
1 ##
2 # User Database
3 #
4 # Note that this file is consulted directly only when the system is running
5 # in single-user mode. At other times this information is provided by
6 # Open Directory.
7 #
8 # See the opendirectoryd(8) man page for additional information about
9 # Open Directory.
10 ##
11 nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false正如我们所看到的,对第10行进行注释,我希望得到一些命令,如
$ cat -n [11:] /etc/passwd
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico如何做到这一点?
发布于 2018-03-29 02:33:01
如果您想忽略文件中任何位置的注释行,而不需要对它们进行计数,则应该这样做:
grep -n -v ^# /etc/passwdgrep的-n选项与cat一样,对行进行编号(虽然输出格式略有不同,但grep在行号和内容之间添加了冒号,也没有填充数字)。
-v选项告诉grep打印与正则表达式不匹配的行。
正则表达式^#只在行的开头匹配文字#。
如果您想要的是始终跳过前10行,那么tail +11应该这样做。您可以通过管道将cat -n输送到它:
cat -n /etc/passwd | tail +11有关更多详细信息,请参见tail的手册页,更具体的是选项-n (可以省略,如这里所示)。
https://unix.stackexchange.com/questions/434204
复制相似问题