LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sorting a csv file school project (http://www.linuxquestions.org/questions/linux-newbie-8/sorting-a-csv-file-school-project-4175430555/)

Deke602 10-04-2012 04:00 PM

Sorting a csv file school project
 
I have a final project I need to complete so I can move on to the next semester. It would be greatly appreciated if someone could help with my final project. So here is my third question I'm working on.

Using a csv file, provide a single command line that will list the number of times each city name is present, in descending order. An example of the output:
2 Globe
1 Bisbee

Here are some of the entries of the file:

85640,Agua Linda,520

85320,Aguila,520

85321,Ajo,520

86506,Allentown,928

85920,Alpine,928

85640,Amado,520

85645,Amado,520

85086,Anthem,602

85117,Apache Jct,480

85118,Apache Jct,480

85119,Apache Jct,480

85120,Apache Jct,480

85178,Apache Jct,480

85217,Apache Jct,480

85218,Apache Jct,480

85219,Apache Jct,480

85220,Apache Jct,480

85278,Apache Jct,480

85290,Apache Jct,520

85117,Apache Junction,480

85118,Apache Junction,480

85119,Apache Junction,480

85120,Apache Junction,480

85178,Apache Junction,480

85190,Apache Junction,520

85217,Apache Junction,480

85218,Apache Junction,480

85219,Apache Junction,480

85220,Apache Junction,480

85278,Apache Junction,480

85290,Apache Junction,520

85601,Arivaca,520

85123,Arizona City,480

85223,Arizona City,520

85724,Arizona Medical Center,520

85099,Arizona State Lottery,602

85287,Arizona State University,480

85322,Arlington,623

86320,Ash Fork,928

85287,ASU,480

85323,Avondale,623

85392,Avondale,520

85392,Avondale Goodyear,520

85395,Avondale Goodyear,520

85323,Avondale-Goodyear,623

85338,Avondale-Goodyear,623

86033,Baby Rock,928

86030,Bacobi,928

86321,Bagdad,928

85121,Bapchule,480

85221,Bapchule,520

85607,Bay Acres,520

86432,Beaver Dam,928

86015,Bellemont,928

86333,Bensch Ranch,928

85602,Benson,520

85603,Bisbee,520

85603,Bisbee Jct,520

86031,Bitahochee,928

85324,Black Canyon City,928

85324,Black Cyn City,928

86033,Black Mesa,928

85922,Blue,928

86520,Blue Gap,928

85643,Bonita,520

85325,Bouse,928

85605,Bowie,520

85326,Buckeye,623

85396,Buckeye,623

86426,Bullhead City,928

86427,Bullhead City,928

86429,Bullhead City,928

86430,Bullhead City,928

86439,Bullhead City,928

86442,Bullhead City,928

86446,Bullhead City,928

86512,Burnt Water,928

85775,Business Reply,520

85530,Bylas,928

86016,Cameron,928

86020,Cameron,928

86322,Camp Verde,928

86022,Cane Beds,928

85611,Canelo,520

86503,Canyon de Chelly National Mo,928

85377,Carefree,480

85640,Carmen,520

85122,Casa Grande,480

85123,Casa Grande,480

85130,Casa Grande,520

85193,Casa Grande,520

85194,Casa Grande,520

85222,Casa Grande,520

85230,Casa Grande,520

85293,Casa Grande,928

85294,Casa Grande,928

85602,Cascabel,520

85329,Cashion,623

85738,Catalina,520

85739,Catalina,520

85327,Cave Creek,480

85331,Cave Creek,480

86020,Cedar Ridge,928

85097,Census Bureau,602

85531,Central,928

86502,Chambers,928

85224,Chandler,480

85225,Chandler,480

85226,Chandler,480

85244,Chandler,480

85246,Chandler,480

85248,Chandler,480

85249,Chandler,480

85286,Chandler,480

85127,Chandler Heights,480

85227,Chandler Heights,480

85242,Chandler Heights,480

86033,Chilchinbito,928

86503,Chinle,928

86507,Chinle,928

86538,Chinle,928

86545,Chinle,928

86547,Chinle,928

86556,Chinle,928

86323,Chino Valley,928

85643,Chiricahua National Monument,520

86431,Chloride,928

85911,Cibecue,928

85328,Cibola,928

85342,Circle City,520

86324,Clarkdale,928

85923,Clay Springs,928

85532,Claypool,928

85533,Clifton,928

86045,Coal Mine Mesa,928

85606,Cochise,520

TB0ne 10-04-2012 04:12 PM

Quote:

Originally Posted by Deke602 (Post 4797493)
I have a final project I need to complete so I can move on to the next semester. It would be greatly appreciated if someone could help with my final project. So here is my third question I'm working on.

Using a csv file, provide a single command line that will list the number of times each city name is present, in descending order. An example of the output:
2 Globe
1 Bisbee

Here are some of the entries of the file:

We are always happy to HELP if you're stuck with your homework...but you do have to show some effort of your own. What have you done/tried so far? If you've not tried anything yet, I would look at the man pages for the awk, uniq, and sort commands.

Specifically:
  • Pay attention to the -F flag in the awk man page
  • Pay attention to the -c flag in the uniq man page
  • Pay attention to the -b, -g, and -r flags in the sort man page.

propofol 10-04-2012 04:18 PM

Look up "cut", "sort" & "uniq".

I am not that familiar with awk, however I believe that will do it as well.

Regards,
Stefan

TB0ne 10-04-2012 04:31 PM

Quote:

Originally Posted by propofol (Post 4797503)
Look up "cut", "sort" & "uniq".
I am not that familiar with awk, however I believe that will do it as well.

Either cut or awk will work to parse things apart based on the comma.

Deke602 10-04-2012 04:33 PM

This is what I've been using...
 
Quote:

Originally Posted by TB0ne (Post 4797500)
We are always happy to HELP if you're stuck with your homework...but you do have to show some effort of your own. What have you done/tried so far? If you've not tried anything yet, I would look at the man pages for the awk, uniq, and sort commands.

Specifically:
  • Pay attention to the -F flag in the awk man page
  • Pay attention to the -c flag in the uniq man page
  • Pay attention to the -b, -g, and -r flags in the sort man page.

I have been putting effort for the past three days bro!
I have used this sort -r | uniq -c -f2 az.csv
No go gives me no sort on the count and the city name. I know nothing about awk. I have several more questions I have to do so if this is easy for you how bout you hook me up with the code. I'm trying to make it to next semester not just pass a classes I have 5 others I'm working on too.

TB0ne 10-04-2012 04:53 PM

Quote:

Originally Posted by Deke602 (Post 4797513)
I have been putting effort for the past three days bro!
I have used this sort -r | uniq -c -f2 az.csv
No go gives me no sort on the count and the city name. I know nothing about awk. I have several more questions I have to do so if this is easy for you how bout you hook me up with the code. I'm trying to make it to next semester not just pass a classes I have 5 others I'm working on too.

I am not your "bro", and 'how bout you' think about how you're phrasing things.

And I essentially gave you the answer...if you read the man pages, and pay attention to what those flags do, you'll have your answer. Did you read the man pages, and read what those flags do?? propofol also suggested the cut command instead of awk, which will also work. The flags on the cut command would be "-d" and "-f". Break the problem down:
  • You want the second field, (city) right?? So use cut or awk to get it.
  • After that, you want to count the unique names...use uniq to do this
  • Then sort it, biggest values on top...use the sort command
This is your homework...you've been handed the options to use.
Code:

cut -d , -f 2 test.csv | uniq -c | sort -bgr
Next time, try reading what's suggested to you, and tell us what you've tried up front.

Deke602 10-04-2012 05:17 PM

Question 3...
 
Ok my bad and maybe your not a bro, my bad once again. Anyhow, I've been trying all sorts of combinations and nothing would sort the count. I'll give this a try and I should've posted what I tried. I've been reading the man pages and my book. So i tried to figure it out on my own with no help and so far I think I've been doing pretty good. I'll give it one more try and see if yours works. Thank you I do greatly appreciate all your help. Just a little frustrated. I'll reply with what happens next in a bit.

Deke602 10-04-2012 05:50 PM

Yes...
 
That was exactly it! I was on the right track. I used those commands once before but I had them in the wrong order. I appreciate the help. Now I'm onto the question. But going to scrap this one out on my own for a bit and see how far I get until the frustration sets in.

TB0ne 10-05-2012 10:16 AM

Quote:

Originally Posted by Deke602 (Post 4797568)
That was exactly it! I was on the right track. I used those commands once before but I had them in the wrong order. I appreciate the help. Now I'm onto the question. But going to scrap this one out on my own for a bit and see how far I get until the frustration sets in.

No worries, and glad it helped. We're always happy to help folks with homework questions, but the only caveat we've got here is that you have to show you've tried, which you did. :)

Breaking a task down like this is always the best way to get it done. Once you figure out the what needs to happen, the commands are usually easy to follow after that. You were definitely on the right track, though.


All times are GMT -5. The time now is 06:16 PM.