LinuxQuestions.org
Review your favorite Linux distribution.
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, 04: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 05:37 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-20-2012, 05:31 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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, 08:58 PM   #3
Trd300
Member
 
Registered: Feb 2012
Posts: 89

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

Last edited by Trd300; 05-01-2012 at 05:37 AM.
 
Old 03-20-2012, 10: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 02:05 AM.
 
Old 03-21-2012, 02:22 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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, 02: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, 03:16 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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, 10:18 PM   #8
Trd300
Member
 
Registered: Feb 2012
Posts: 89

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

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

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

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

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

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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, 04:55 AM   #12
Trd300
Member
 
Registered: Feb 2012
Posts: 89

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

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:08 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 11:19 AM
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 01:59 AM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 11:15 PM


All times are GMT -5. The time now is 10:16 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration