![]() |
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 |
Quote:
Specifically:
|
Look up "cut", "sort" & "uniq".
I am not that familiar with awk, however I believe that will do it as well. Regards, Stefan |
Quote:
|
This is what I've been using...
Quote:
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. |
Quote:
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:
Code:
cut -d , -f 2 test.csv | uniq -c | sort -bgr |
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.
|
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.
|
Quote:
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 01:22 PM. |