LinuxQuestions.org
Visit Jeremy's Blog.
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 09-26-2017, 11:06 AM   #1
JUSTIN3499
LQ Newbie
 
Registered: Sep 2017
Posts: 2

Rep: Reputation: Disabled
Post Linux help: using a command to list certain categories in a directory with the number of employees


So for instance refer to this data.

004,Hendrix,James,Engineering,9
237,Cauthon,Mat,Marketing,2
889,Wallace,Mia,Accounting,8
096,Uffhark,Joan,Administration,9
120,Carrington,Blake,Accounting,8
053,Draper,Don,Marketing,3
413,Zappa,Francesco,Engineering,9
285,Gunvalson,Victoria,Engineering,9
744,Watts,Charles,Administration,8
409,Wilson,Brian,Engineering,9
346,McCormick,Kenneth,Marketing,2
070,Berlin,Rachel,Marketing,2
555,Zombie,Robert,Administration,7
217,Burns,Fahr,Accounting,8
154,Tennille,Antoinette,Engineering,9
843,Trakand,Elayne,Administration,8
654,Grumby,Jonas,Administration,7
026,Corleone,Michael,Marketing,3
599,March,Christopher,Engineering,9
808,Mann,Yoda,Engineering,9

Now how would i make a list of data showing only the department and the total number of employees it has. For instance how would I would a Linux command to show the Engineering department and how many people work in that department based off the data? What i am concluding is using the "awk" command but im not for sure. someone please help.
 
Old 09-26-2017, 11:24 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,099

Rep: Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835Reputation: 7835
Quote:
Originally Posted by JUSTIN3499 View Post
So for instance refer to this data.

004,Hendrix,James,Engineering,9
237,Cauthon,Mat,Marketing,2
889,Wallace,Mia,Accounting,8
096,Uffhark,Joan,Administration,9
120,Carrington,Blake,Accounting,8
053,Draper,Don,Marketing,3
413,Zappa,Francesco,Engineering,9
285,Gunvalson,Victoria,Engineering,9
744,Watts,Charles,Administration,8
409,Wilson,Brian,Engineering,9
346,McCormick,Kenneth,Marketing,2
070,Berlin,Rachel,Marketing,2
555,Zombie,Robert,Administration,7
217,Burns,Fahr,Accounting,8
154,Tennille,Antoinette,Engineering,9
843,Trakand,Elayne,Administration,8
654,Grumby,Jonas,Administration,7
026,Corleone,Michael,Marketing,3
599,March,Christopher,Engineering,9
808,Mann,Yoda,Engineering,9

Now how would i make a list of data showing only the department and the total number of employees it has. For instance how would I would a Linux command to show the Engineering department and how many people work in that department based off the data? What i am concluding is using the "awk" command but im not for sure. someone please help.
What have you done/tried so far?? And read the "Question Guidelines" and LQ Rules, please...we're happy to help you, but posting a homework question without showing your own efforts isn't going to get you much.

Awk is complex and can do quite a bit. You can also accomplish this with a bash script, using grep and other things as well. Perl, python, and any other scripting/programming language can also be used. You also don't tell us what your data above actually is, or where it comes from.
 
Old 09-26-2017, 11:32 AM   #3
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 15,433

Rep: Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160Reputation: 2160
Why not grep for the various departments?
 
Old 09-26-2017, 11:39 AM   #4
JUSTIN3499
LQ Newbie
 
Registered: Sep 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
I have tried awk to cut out specific fields in the directory and list only the fields I wants for instance awk -F "," '{print $4}' labdata | grep Engineering | wc -l. But when i do that it only list the number of times it shows up for instance "7" is what pops up I dont know how to get the word Engineering into the command line with it.
 
Old 09-26-2017, 12:17 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,560

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
You can count each department in an string-addressed array (hash).
At the END print each department with its count.
Code:
awk -F "," '{cnt[$4]++} END { for (i in cnt) print i,cnt[i] }' labdata
The for loop lets i cycle through each (string-)index of cnt[]
 
Old 09-26-2017, 05:56 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,944

Rep: Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070Reputation: 4070
That counts the number of instances of each department in the list, not the total employes, but is certainly an excellent starting point for the OP to resolve the issue.
 
  


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
using spawn command to find the number of files in a directory. jithuchandran Linux - Newbie 5 12-08-2014 11:54 AM
Getting a directory list with a line specifying the number of filenames displayed. stf92 Slackware 12 06-01-2013 03:25 AM
shell script to seperate male employees from female employees sarahm Linux - Newbie 11 05-11-2012 03:36 PM
ls command and displaying the number of files in your current directory? revsarah Linux - General 5 10-15-2010 06:40 PM
Command to list total number of files. WillieB_72 Linux - General 3 01-29-2003 09:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:47 AM.

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
Open Source Consulting | Domain Registration