cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Starfighter Starfighter
Starfighter
  • 299 Views

Using awk

Jump to solution

Using the awk tool, write the command that will search through the /etc/passwd
file for an account with the username of "trev1".  If the account name exist, the
command should output the login directory for this account.

Note:  The use of pipes is NOT permitted!!!!

 

Trevor "Red Hat Evangelist" Chandler
Labels (3)
1 Solution

Accepted Solutions
Chetan_Tiwary_
Community Manager
Community Manager
  • 209 Views

just to help the learners, here is the explanation of the command and what it does :

First let us analyze the fields in the /etc/passwd file :

Chetan_Tiwary__0-1730288444024.png

same way, as per the question's condition : the login directory of such users is in the 6th field of the /etc/passwd file

trev1:x:1001:1001:Trev User:/home/trev1:/bin/bash

1        2    3      4           5                  6                   7

 

so, this command when we use it : awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd

awk -F ':' sets the field separator to a colon (:) character. This instructs awk to interpret each line of the input file as a sequence of fields, delimited by ':'.

$1 == "trev1" { print $6 }: The condition $1 == "trev1" checks if the first field of the current line, which represents the username in the /etc/passwd file, matches "trev1".

and then If the username matches "trev1", the command prints the sixth field, which corresponds to the user's home directory in the /etc/passwd file.

and done !!

Chetan_Tiwary__1-1730289743816.png

View solution in original post

3 Replies
TM
Flight Engineer Flight Engineer
Flight Engineer
  • 282 Views

awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd

Trevor
Starfighter Starfighter
Starfighter
  • 257 Views

And we have a winner!!!!  That command syntax will 
absolutely accomplish the task!!!

Great job TM!!!

 

Another form that would get the job done is:

awk  -F:  ' /trev1/  { print $6 }'  /etc/passwd

Trevor "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 210 Views

just to help the learners, here is the explanation of the command and what it does :

First let us analyze the fields in the /etc/passwd file :

Chetan_Tiwary__0-1730288444024.png

same way, as per the question's condition : the login directory of such users is in the 6th field of the /etc/passwd file

trev1:x:1001:1001:Trev User:/home/trev1:/bin/bash

1        2    3      4           5                  6                   7

 

so, this command when we use it : awk -F ':' ' $1 == "trev1" { print $6 } ' /etc/passwd

awk -F ':' sets the field separator to a colon (:) character. This instructs awk to interpret each line of the input file as a sequence of fields, delimited by ':'.

$1 == "trev1" { print $6 }: The condition $1 == "trev1" checks if the first field of the current line, which represents the username in the /etc/passwd file, matches "trev1".

and then If the username matches "trev1", the command prints the sixth field, which corresponds to the user's home directory in the /etc/passwd file.

and done !!

Chetan_Tiwary__1-1730289743816.png

Join the discussion
You must log in to join this conversation.