Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, This is going to sound like homework, because it is. I have been trying for a couple hours and cant figure it out, I have read every faq on ...
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    2

    Reordering Field durring Sort

    Hello,
    This is going to sound like homework, because it is. I have been trying for a couple hours and cant figure it out, I have read every faq on sort.

    I need to take a file formated as the following
    first:last:city:state

    sort it by last name

    Display only the ones from a certain state and display it in this order

    last:first:city:state

    Normally I wouldn't post homework, but this is a project, and I am STUMPED! I have hit a brick wall in my script.


    Any help is greatly appreciated.

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Hints:
    awk [ study it @ Awk - A Tutorial and Introduction - by Bruce Barnett ]
    sort (since you have read the faqs about sort I believe you have ideas how to sort by specific column )

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Okay, to me this is really fun (I love Unix text processing). Here is a fairly easily-grasped way to do it, but it uses multiple programs. Sadly I don't have access to a "real" Operating System at this moment, so I am doing this by memory.

    Code:
    sed -n "/$STATE/p
    This uses sed, the stream editor. You tell it not to print with -n, then you pass it its arguments, namely to print (p) lines containing $STATE. Obviously you would replace $STATE with the state you need to filter out.

    As for the sorting by last name, you use sort for this (note: this might not work, as my knowledge is fuzzy here)

    Code:
    sort -t ":" +1
    I believe that this tells sort to use : as a delimiter, and to sort by the second column.
    Thus you would just pipe these two commands together and send it to some output.

  4. #4
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    I'm sorry, I think the correct syntax is
    sort -t : -k 2

    To sort by the second column using : as the delimiter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...