awk sort

input file

abc1
abc23
abc12
abc15

output

abc1
abc12
abc15
abc23

Why not simply
sort

why do you need awk to sort ?

you can simply use the sort command

$ sort input.txt
abc1
abc12
abc15
abc23

actually it does NOT work

you can try

abc1
abc12
abc15
abc2
abc23

---------- Post updated at 08:06 AM ---------- Previous update was at 08:06 AM ----------

actually it does NOT work

you can try

abc1
abc12
abc15
abc2
abc23
sort -k 1.4n infile

You should start checking the man page for commands.

Well, you just changed the sample!

What are you trying to have happen?

I have man sort it says

       -k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default end of line)

BUT, I don't understand the "1" "." "4", how did you get them?

You did not answer, why it must be awk as stated in the subject.

Count from the start of the string and check what character is at position 4 while keeping in mind, that "n" stands for numeric. The 1 should explain itself as no delimeter is defined.

Thanks for the explaination. I use awk becasue I think awk can do this thing, I may get some learning point from the "awk answer" :slight_smile:

You could do it in AWK, but you would essentially be reimplementing sort (read, compare, sort, output). If you think there's something to be learned from that exercise, it would defeat its purpose if we did it for you.

Regards,
Alister

1 Like