INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

AWK - Sorting

AWK - Sorting

(OP)
Hello,
I would like to sort the following text:

CODE -->

GRID         112        1413.987-822.42180.59229
GRID         108         1453.57-820.634 79.0209
GRID         109        1457.868-820.76280.09176
GRID         111        1454.621-819.14574.98186
GRID         113        1408.708-822.44679.95545
GRID         110        1458.919-819.27476.05272 
like this:

CODE -->

GRID         108         1453.57-820.634 79.0209
GRID         109        1457.868-820.76280.09176
GRID         110        1458.919-819.27476.05272
GRID         111        1454.621-819.14574.98186
GRID         112        1413.987-822.42180.59229
GRID         113        1408.708-822.44679.95545 
How can I do it using awk, sorting the field comprised by the digits 9 to 16.
Thank you

RE: AWK - Sorting

the unix shell command sort can do this quite easily - any reason why you need to do awk?
(for help with the unix sort command, type man sort at a unix/linux command prompt).

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile big data clouds)


RE: AWK - Sorting

(OP)
Hi Johnherman,

There is no reason to use awk, but I would like to learn how to do it using this language (it seems a problem for awk).
By the way, can you post the solution using unix shell? Thank you

RE: AWK - Sorting

Hi

Depends on what kind of Awk implementation you have. GNU Awk has some proprietary functions provided for such tasks, but in other implementations you may need to code the sorting by hand. ( man mawk has an example code for this. )

You seems to need simple sorting by text of each line, for which asort() is enough :

CODE --> GNU Awk

{
    a[NR] = $0
}
END {
    n = asort(a)
    for (i = 1; i < n; i++)
        print a[i]
} 

If you want to sort by the value of a certain column, for distinct values is easy to solve it with asorti() :

CODE --> GNU Awk

{
    a[$2] = $0
}
END {
    n = asorti(a, o)
    for (i = 1; i < n; i++)
        print a[o[i]]
} 

But if you want to sort by a column and there may be duplicated values, things become more complicated.

Feherke.
feherke.ga

RE: AWK - Sorting

(OP)
Thank you for your answer feherke, since I am not so experienced with this language, could you tell me how to read the file "grids.txt" together with awk? Normally I use awk language in the linux terminal like this:

CODE -->

>> cat file.txt | awk '{printf("%s\n",$1)}' 

Hi PHV, regarding your comment, I am learning awk in my free times, being on an early stage I dont have code where I am stuck. Currently, I would like to expose just some questions and see some different approaches to solve a given problem. I think, it constitutes a valuable source for me and for all the users of this forum.



RE: AWK - Sorting

Hi

Exactly like that. Maybe avoiding the UUOC :

CODE --> command line

master # awk '{a[NR]=$0}END{n=asort(a);for(i=1;i<n;i++)print a[i]}' grids.txt
GRID         108         1453.57-820.634 79.0209
GRID         109        1457.868-820.76280.09176
GRID         110        1458.919-819.27476.05272
GRID         111        1454.621-819.14574.98186
GRID         112        1413.987-822.42180.59229 

Or put the code I posted earlier in a file, for example grids.awk and call it like this :

CODE --> command line

master # awk -f grids.awk grids.txt
GRID         108         1453.57-820.634 79.0209
GRID         109        1457.868-820.76280.09176
GRID         110        1458.919-819.27476.05272
GRID         111        1454.621-819.14574.98186
GRID         112        1413.987-822.42180.59229 

Or ask the system where your Awk interpreter is ( which awk ) and add a shebang pointing to it in grids.awk's first line :

CODE --> grids.awk

#!/usr/bin/awk -f

{
    a[NR] = $0
}

END {
    n = asort(a)
    for (i = 1; i < n; i++)
        print a[i]
} 
... then make the file executable ( chmod +x grids.awk ) and run it :

CODE --> command line

master # ./grids.awk grids.txt 
GRID         108         1453.57-820.634 79.0209
GRID         109        1457.868-820.76280.09176
GRID         110        1458.919-819.27476.05272
GRID         111        1454.621-819.14574.98186
GRID         112        1413.987-822.42180.59229 

Of course, if you move ( or symlink ) grids.awk into one of the directories enumerated in your PATH environment variable, you will be able to run it without the ./ ( or whatever else ) path.

Feherke.
feherke.ga

RE: AWK - Sorting

(OP)
Thank you for your reply Feherke!

I still have some questions regarding your script:
1- Why I don't need to use "BEGIN" in this case?

2- How do the script knows that I am sorting the lines regarding the field 2?

RE: AWK - Sorting

Hi

Quote (nvhuser)

1- Why I don't need to use "BEGIN" in this case?
Well, the code's logic requires nothing to be done before processing any input line.

Theoretically in our case array a's declaration could be in the BEGIN block, but no such thing exists in Awk.

Quote (nvhuser)

2- How do the script knows that I am sorting the lines regarding the field 2?
The first code I posted does not. It works for your sample input because the 1st column is identical, so in this case sorting by whole lines and sorting 2nd column have the same result.

The second code I posted sorts by 2nd column as array a is indexed by $2 then the array is sorted nut by values, but by indexes. ( As mentioned, that indexing by $2 has the drawback that $2 has to be unique. )

Feherke.
feherke.ga

RE: AWK - Sorting

(OP)
Hi Feherke, thank you for your contribution and your detailed answers.
I will keep learning awk!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close