LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-20-2012, 03:53 AM   #1
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Rep: Reputation: Disabled
awk: sort variable values and assign a name accordingly


%%%%%

Last edited by Trd300; 05-01-2012 at 04:37 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-20-2012, 04:31 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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)
 
Old 03-20-2012, 07:58 PM   #3
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
%%%%%

Last edited by Trd300; 05-01-2012 at 04:37 AM.
 
Old 03-20-2012, 09:10 PM   #4
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
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) !

Last edited by Trd300; 03-21-2012 at 01:05 AM.
 
Old 03-21-2012, 01:22 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
asort - sort the values of the array, ie. a[1] = 2 and a[2] = 1, once sorted the new array will be,
Code:
asort(a, b)
b[1] = 1 and b[2] = 2
asorti - sort the indexes within the array, ie. you create an array a[7] = blah and a[3] = foo, as it is now the indexes being sorted you will have,
Code:
asort(a, b)
b[1] = 3 and b[2] = 7
The trick here is that to know get the original values from 'a' you will need to do:
Code:
print a[b[1]]
This will output 'foo'

As for creating an array equal to the alphabet, the simplest is:
Code:
split("abcd...", c2, "")
This will create array c2 starting at index 1 each with a value from the string.

So, using the above and something similar to the following logic:
Code:
awk -F"\t" '{n = split($2, a, "[ ;]+");asort(a, b);for(i=1;i<=(n-(n/3));i++)print i,b[i]}' file
Obviously change the print to be the assigning of the letters.

The trick now will be how you get it back in the correct order
 
2 members found this post helpful.
Old 03-21-2012, 01:53 AM   #6
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Thanks grail !

Now I understand how these functions work.

Just another question about asort: Does it sort only numerical values?

In my case, the values "AGE" will be sorted before/after the numbers? Or discarded maybe?
 
Old 03-21-2012, 02:16 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
No everything is sorted, but as numbers are prior to the alphabet they will be in the first set of indexes
 
Old 03-21-2012, 09:18 PM   #8
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
%%%%%

Last edited by Trd300; 05-01-2012 at 04:38 AM.
 
Old 03-22-2012, 12:52 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 03-22-2012, 02:15 AM   #10
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
%%%%%

Last edited by Trd300; 05-01-2012 at 04:38 AM.
 
Old 03-22-2012, 03:35 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
How about:
Code:
awk -F"\t" 'BEGIN{OFS=FS="\t";split("abcdefghijk",letters,"")}{split($2, a, "[ ;]+");n = asort(a, b);f3 = gensub(/AGE /,"","g",$2);for(i=1;i<=(n-(n/3));i++)sub(b[i],letters[i],f3);$3=f3}1' file
 
1 members found this post helpful.
Old 03-22-2012, 03:55 AM   #12
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
%%%%%

Last edited by Trd300; 05-01-2012 at 04:38 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help with awk and accessing variable values within the syntax meridionaljet Linux - Software 4 07-09-2011 01:08 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 12:59 AM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 10:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration