The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Bash & grep question

clsCorwinclsCorwin Registered User regular
So I have a bunch of data that looks like this:
Country: (Unknown Country?) (XX) City: (Unknown City?) IP: 112.78.119.61
Country: CHINA (CN) City: Beijing IP: 114.247.18.5
Country: HONG KONG (HK) City: Hong Kong IP: 115.126.5.104
And I want to arrange it nice and alphabetically by country.

So I tried this as a shot in the dark:
ALPHA="( a b c d e f g h i j k l m n o p q r s t u v w x y z"
#grep in geoip_list for 'Country: [$x].*
for x in $ALPHA
do
  grep 'Country: [$x].*' geoip_list >> geo_alphabetical
done

but it didn't work. Can I do this, or do I just have to do individual greps for each letter?

clsCorwin on

Posts

  • ecco the dolphinecco the dolphin Registered User regular
    edited November 2010
    Hmmm... have you considered using sort as an alternative?

    Edit:

    e.g. sort geoip_list > geo_alphabetical

    Alternatively, if you have lines other than country and only want the lines starting with Country:

    grep '^Country: ' geoip_list | sort > geo_alphabetical

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • clsCorwinclsCorwin Registered User regular
    edited November 2010
    I actually tried sort first, but since every line starts with Country: the sort does no good. If I could sort based on a specific field, that would work.

    clsCorwin on
  • ecco the dolphinecco the dolphin Registered User regular
    edited November 2010
    clsCorwin wrote: »
    I actually tried sort first, but since every line starts with Country: the sort does no good. If I could sort based on a specific field, that would work.

    Could you double check, please? A quick test here shows that sort sorts by the entire line, as opposed to fields:
    $ cat geoip_list 
    Country: HONG KONG (HK) City: Hong Kong IP: 115.126.5.104
    Country: CHINA (CN) City: Beijing IP: 114.247.18.5
    Country: (Unknown Country?) (XX) City: (Unknown City?) IP: 112.78.119.61
    
    $ sort geoip_list 
    Country: (Unknown Country?) (XX) City: (Unknown City?) IP: 112.78.119.61
    Country: CHINA (CN) City: Beijing IP: 114.247.18.5
    Country: HONG KONG (HK) City: Hong Kong IP: 115.126.5.104
    

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • clsCorwinclsCorwin Registered User regular
    edited November 2010
    hmm, maybe I just mistyped something last time causing it to not work.

    Lets see how it pans out here in a few.

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited November 2010
    Sorted good, now it just doesnt want to display right. Rather than getting:
    Country: HONG KONG (HK) City: Hong Kong IP: 115.126.5.104

    I have
    Country:
    HONG
    KONG
    (HK)
    City:
    ...

    argh

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited November 2010
    Ok, so I have the geo_alphabetical file with all the data, which needs to get dumped into an html file.

    right now, I have $HDR defined as my header for the html file, and $FTR likewise for the footer.

    before, when I just dumped the data straight into the html after the lynx query for groip info, it came out as one solid line. Trying to cat or echo the file to the html file results in word by word messiness.

    How should I do this?

    clsCorwin on
  • clsCorwinclsCorwin Registered User regular
    edited November 2010
    Nevermind, fixed it by appending to the file at the sort. You know, the way it should have been done the first time.

    clsCorwin on
Sign In or Register to comment.