0

I spotted the solution to this particular AWK programming question AWK to find Top 3 values and I wondered how the AWK program could be modified to output the top three values by each site (ie $1) as opposed to the top three values overall? I'm guessing this means either an additional array or loop that cycles through the values in $1 to create the top three rankings?

The input file would look something like this;-

Surrey,Guildford,East,1
Surrey,Guildford,West,2
Surrey,Guildford,North,3
Surrey,Guildford,South,2
Surrey,Weybridge,East,2
Surrey,Weybridge,North,3
Surrey,Weybridge,West,3
Surrey,Weybridge,South,1
Devon,Exeter,North,1
Devon,Exeter,South,2
Devon,Exeter,East,4
Devon,Exter,West,2

Then the output should look like this;-

Surrey,Guildford,North,3
Surrey,Guildford,West,2
Surrey,Guildford,South,2
Surrey,Weybridge,North,3
Surrey,Weybridge,West,3
Surrey,Weybridge,East,2
Devon,Exeter,East,4
Devon,Exeter,South,2
Devon,Exeter,West,2
3
  • 1
    For us to best be able to help you, please edit your question to include concise, testable sample input, expected output, and the code you want help with plus your attempt to solve the problem yourself. No links, no images, just text.
    – Ed Morton
    Jul 21 at 15:00
  • 1
    your expected output doesn't obeying the saying you said "output the top three values by each site (ie $1)", for example top 3 for site name Surrey are Surrey,Guildford,North,3, Surrey,Weybridge,North,3 and Surrey,Weybridge,West,3. so please clarify in your question by editing how you chose others for this site? and what if there was more than 3 top for the same site name? which you will chose? the first one that seen or the last one or all? for example if there was one Surrey,Weybridge,South,3 in the input too. your input is sorted on site name? Jul 21 at 15:28
  • 3
    Thanks for adding sample input/output, please also edit your question to show your attempt to solve the problem yourself. Also, please change the subject line from "Modifying existing AWK program" to something that represents the actual problem you're trying to solve by modifying an awk program (which, having glanced at the linked question, may not be the best approach to YOUR problem) so people with a similar problem can find this question (and hopefully an associated answer) in future.
    – Ed Morton
    Jul 21 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.