Sponsored Content
Top Forums Shell Programming and Scripting awk - Why does output order change? Post 302882901 by treesloth on Thursday 9th of January 2014 03:22:31 PM
Old 01-09-2014
awk - Why does output order change?

I have this END section in an awk script:


Code:
END     {
                for (j in littlebin)
                        {
                        print (j, int(log(j)/log(2)) , littlebin[j])
                        lbsum+=littlebin[j]
                        }

                for (i in bins)
                        {
                        print (i / (1024^2) "MB" , bins[i])
                        }
        }

As you can see I have 2 for loops, each outputting the contents of an array, with a little extra calculation. So, it prints in this order:

(1) Contents of the littlebin array, then
(2) Contents of the bins array

Ok, good. But, I'd like the contents of each of those arrays to be sorted numerically on the first element printed. So, I add a sort pipe:

Code:
END     {
                for (j in littlebin)
                        print (j, int(log(j)/log(2)), littlebin[j]) | "sort -nk 1"
                        lbsum+=littlebin[j]

                for (i in bins)
                        print (i / (1024^2) "MB" , bins[i]) | "sort -nk 1"
        }

This causes the output to be somewhat reversed, to become:

(1) Contents of the bins array, numerically sorted, then
(2) Contents of the littlebin array, numerically sorted

Why is that happening? And what is the best way of sorting as I'm attempting, on the index of each array? I've tried asorti, but it doesn't sort numerically. Thank you.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies

2. AIX

How to change the order of authentication in aix?

I have an AIX system configured as NIS client. I have an local user on the system called "batman" and i have a user by the same name in NIS as well. Now when i try to login with Batman user, the local batman gets in. How do I tell the AIX machine to authenticate Batman as NIS user? ... (2 Replies)
Discussion started by: balaji_prk
2 Replies

3. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

4. Shell Programming and Scripting

How can I change is output format by awk ?

Hello, Can you tell me how can I change this format by awk Input 0.2057422D-01 0.2463722D-01 -0.1068047D-02 Output 0.02057422 0.02463722 -0.001068047 Thanks wan (8 Replies)
Discussion started by: wanchem
8 Replies

5. Shell Programming and Scripting

Change order

Good evening I have a file as below and want to change the order, as in the second column, sed awk Pearl Thanks aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa cccccccc bbbbbbbbb aaaaaaaaaa cccccccc bbbbbbbbb (8 Replies)
Discussion started by: Novice-
8 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

change order

I have inside a file 22 25 80 111 631 694 861 875 I need this in the form using awk or sed 22,25,80,111,631,694,861,875 (4 Replies)
Discussion started by: anil510
4 Replies

8. UNIX for Dummies Questions & Answers

change service order

hi guys I have a service that depends on some shares (NFS shares ) that need to be mounted before before the service start so the service-app finds the NFS shares and starts correctly... I am confused here this is what I found but I am not sure what to do in order to change it BTW is Suse... (2 Replies)
Discussion started by: karlochacon
2 Replies

9. Shell Programming and Scripting

Change of fields order inline

Hi everyone, What is the best solution to check every line in the xml file and change order of found field along with its value without touching value. Pattern will be given i.e. one line can look like this one: <widget position="value,value" size="value,value" name="value"... (5 Replies)
Discussion started by: TiedCone
5 Replies

10. Shell Programming and Scripting

How to change row by order nr?

Hello, I have a file with thousands of rows and I need to change sequence of lines. Sample file: #NAME #SERVICE 112233 #DESCRIPTION AABBCCDD #SERVICE 738292 #DESCRIPTION FFYYRRTT ... ... ... Desired output: #NAME (5 Replies)
Discussion started by: baris35
5 Replies
All times are GMT -4. The time now is 11:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy