LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What is the correct cut command. (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-the-correct-cut-command-4175511179/)

e0055996 07-15-2014 01:10 PM

What is the correct cut command.
 
How could I use the cut command to extract a list of usernames and login shells from the /etc/passwd file, where the resulting usernames and login shells are separated by a single space. Sort the resulting list in ascending alphabetical order, using the login shell as the primary key, and the username as a secondary key. Store the result in the newly created file~/usershells.txt.
This is the command I attempted cut -d /etc/passwd -k7 -k1 | sort > ~/usershells.txt
my kids say this is why at over 40 something's I should leave alone:confused:

TB0ne 07-15-2014 01:15 PM

Quote:

Originally Posted by e0055996 (Post 5204303)
How could I use the cut command to extract a list of usernames and login shells from the /etc/passwd file, where the resulting usernames and login shells are separated by a single space. Sort the resulting list in ascending alphabetical order, using the login shell as the primary key, and the username as a secondary key. Store the result in the newly created file~/usershells.txt.

my kids say this is why at over 40 something's I should leave alone:confused:

What have you done/tried so far?? I'd suggest by reading the man page on the cut and sort commands...and also referencing your textbook, since this sounds VERY much like a verbatim homework question. Personally, I'd shove the file through awk to just grab the two fields you're interested in, then sort it. Pay particular attention to the "-F' flag for awk.

http://linux.die.net/man/1/cut
http://linux.die.net/man/1/sort

e0055996 07-15-2014 02:06 PM

the cose I attempted Guru.
 
I have tried this code so far cut -d /etc/passwd -k7 -k1 | sort > ~/usershells.txt

jpollard 07-15-2014 02:14 PM

What you want is awk, not cut.
Code:

awk -F: '{print $1, $NF;}' </etc/passwd | sort >usershells.txt
The difficulty with cut is getting the fields specified right. It can be done, but awk is simpler.

smallpond 07-15-2014 02:22 PM

Sort with shell first, then reverse.

Code:

awk -F: '{print $NF, $1;}' </etc/passwd | sort |awk '{print $2, $1}'

e0055996 07-15-2014 03:20 PM

I have to use the "cut" command. When I did it with awk, it said it was wrong.

smallpond 07-15-2014 04:02 PM

Parse error - no antecedent for "it".

jpollard 07-15-2014 04:12 PM

Quote:

Originally Posted by e0055996 (Post 5204358)
I have to use the "cut" command. When I did it with awk, it said it was wrong.

Does this happen to be a homework assignment? (having a automated homework checker would explain why it insists on using "cut" - if so, I refer you to the manpage on how to specify fields to be deleted...)


All times are GMT -5. The time now is 12:45 PM.