LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2014, 02:10 PM   #1
e0055996
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: Disabled
Exclamation 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

Last edited by e0055996; 07-15-2014 at 03:05 PM. Reason: input command I attempted
 
Old 07-15-2014, 02:15 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by e0055996 View Post
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
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
 
Old 07-15-2014, 03:06 PM   #3
e0055996
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
the cose I attempted Guru.

I have tried this code so far cut -d /etc/passwd -k7 -k1 | sort > ~/usershells.txt
 
Old 07-15-2014, 03:14 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
Old 07-15-2014, 03:22 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Sort with shell first, then reverse.

Code:
awk -F: '{print $NF, $1;}' </etc/passwd | sort |awk '{print $2, $1}'
 
Old 07-15-2014, 04:20 PM   #6
e0055996
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have to use the "cut" command. When I did it with awk, it said it was wrong.
 
Old 07-15-2014, 05:02 PM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Parse error - no antecedent for "it".
 
Old 07-15-2014, 05:12 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by e0055996 View Post
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...)

Last edited by jpollard; 07-15-2014 at 05:19 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with the cut command..or should I be using the grep command? ProAm500 Linux - Newbie 15 05-01-2013 05:01 AM
[SOLVED] using cut command dinakumar12 Linux - Newbie 3 02-11-2011 07:11 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 09:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM


All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration