[SOLVED] awk: sort variable values and assign a name accordingly
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hmmm ... not sure I follow the logic? For a start, why would you need a for loop when each field2 can only be broken into a single array?? What I mean is, the values for the c2
array will never change, for each individual line, so a for loop only makes the if run 'a' times for no real reason.
I would also hazard that as 'a' contains the number of pieces the field is split into, this would indicate how many letters you require, so if a = 3 then you only need 'a' and 'b'
if it is 6 then you need 'a', 'b', 'c' & 'd'
You also test if $2 has a space in it ... is there ever a case it does not? (example shows it is not required)
One of my problem is that I don't see how the asort() or asorti() functions work, and if the function assign new indices to the values after sorting them.
It seems that asort() and asorti() sort indices and not values. And it does it vertically (add new lines for every indice) !
As far as asort goes, the best way is to have a go and check the results. What I can tell you is that you need to remember the sort just looks at all items and is not selective of doubles,
just the same as if you had to numbers the same they will both be one after the other, ie. 1 4 4 6
As for your final piece of code, you need to think about what you are passing:
Code:
b[1] = "a"
# so when you do
a[b[1]]
# you are saying
a["a"]
Obviously the 'a' array has no value "a" as an index and hence returns nothing.
Here I would have said you have confused asort and asorti. Only the latter works in the way you are trying to use as it is the actual index (ie. what the "i" stands for) that will give the desired
result.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.