Bash: grep pattern from command output

后端 未结 3 508
南旧
南旧 2021-02-05 07:17

I\'m really new with bash, but it\'s one of the subjects on school. One of the exercises was:

Give the line number of the file \"/etc/passwd\" where the informat

3条回答
  •  野的像风
    2021-02-05 07:57

    You can do this with a single awk invocation:

    awk -v me=$(whoami) -F: '$1==me{print NR}' /etc/passwd
    

    In more detail:

    • the -v creates an awk variable called me and populates it with your user name.
    • the -F sets the field separator to : as befits the password file.
    • the $1==me only selects lines where the first field matches your user name.
    • the print outputs the record number (line).

提交回复
热议问题