0

I'd like to check /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

As we see, the firt 10 lines are commented, the result I desire some command like

    $ 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

How to accomplish it?

Wizard
  • 2,503

1 Answers1

1

If you want to ignore the commented lines, anywhere in the file, without having to count them, then this should do it:

grep -n -v ^# /etc/passwd

The -n option to grep does the same as to cat, number the lines (though the output format is slightly different, grep adds a colon between the line number and the contents and also doesn't pad the numbers.)

The -v option tells grep to print the lines that do not match the regular expression.

And the regular expression ^# matches a literal # only at the beginning of the line.

If what you wanted, instead, was to always skip the first 10 lines, then tail +11 should do it. You can pipe cat -n to it:

cat -n /etc/passwd | tail +11

See the man page of tail for more details, more specifically the option -n (which can be omitted, like here.)

filbranden
  • 21,751
  • 4
  • 63
  • 86