LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-26-2012, 06:08 AM   #46
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235

Why do you have to fuse them, when things would become more readable if functions are separated? Clearly it's harder to read now. And sorry, like I said I don't know what's best to do with $n to modify the contents of $0. For me it's a hack. Modifying and resetting the values of RS, FS, etc back and forth around the code would keep things messy.

I was actually wanting to present another for of my follow-up suggestion but I no longer know what's happening here. I thought you wanted each function to be separate so that you could manipulate them easily as you please.
 
Old 09-26-2012, 06:58 AM   #47
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
I am probably wrong, but I though that fusing the 2 functions together into one single function would produce one result only, for which I could assign an unique variable that I can use for the rest of the script.
I don't know if I am right, but with 2 separate functions I have to treat "letter" and "letter_reverse" separately (i.e. assign different variables for each), whereas I don't make distinction between "letter" and "letter_reverse" once they have been produced.
That's why the original idea was to concatenate the results of the function that produces "letter" with the results of the function that produces "letter_reverse", and further process them indifferently like if they were contained in a classical input file.
 
Old 09-26-2012, 01:44 PM   #48
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
So I am not sure I am on track 100%, so i will simply show my idea and see what you think. The main difference is the data is being stored and not written to an intermediate file:
Code:
#!/usr/bin/awk -f

BEGIN{
    FS=OFS="|"
    conv[12]="a"; conv[23]="b"; conv[34]="c"; conv[45]="d"; conv[56]="e"; conv[67]="f"; conv[78]="g"
    conv[87]="h"; conv[76]="i"; conv[65]="j"; conv[54]="k"; conv[43]="l"; conv[32]="m"; conv[21]="n"
    g_count = 1

    printf "Enter max size of data to be stored: "
    getline max < "-"

    if(! max > 0 )max = 0
}

{
    sub(/@/,"")

    getline temp
    split(temp, nums_f, "")
    asort(nums_f, nums_r, "@val_num_desc")

    g_count = convert(data, nums_f, g_count, max)
    g_count = convert(data, nums_r, g_count, max)
}

END{
    print "output"
    for(i=1;i<=length(data);i++)
        print i,data[i]
}

function convert(d_array, n_array, count, max,      i, k, ind)
{
    k = 1
    max = max > 0?(max*2):0

    while( length(n_array) > max ){
        d_array[count] = $1 FS

        for(i = k; i <= length(n_array);i+=2){
            ind = n_array[i] (i+1 in n_array?n_array[i+1]:0)

            d_array[count] = d_array[count] (ind in conv?conv[ind]:"X")
        }
        count++
        k++
        max++
    }

    return count
}
Using the following input file:
Code:
$ cat input_file
@AAA|BBB
12345678
When run at the command line I get:
Code:
$ ./trd300.awk input_file
Enter max size of data to be stored: 2
output
1|AAA|aceg
2|AAA|bdfX
3|AAA|ceg
4|AAA|dfX
5|AAA|hjln
6|AAA|ikmX
7|AAA|jln
8|AAA|kmX
Currently the END is just there to show you the output has been stored in what I think is the correct order??

Also, max is used as greater than the number entered, ie in example entering 2 means all data greater than a length of 2
 
Old 09-26-2012, 07:36 PM   #49
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Thanks grail !

Storing intermediate output in temp instead of a file is also a solution I didn't know how to do.

* Is "temp" a built-in variable? (it is not define anywhere, so I assume yes) What does it refer to? Only the lines without "@" at the beginning?

* What is the third parameter in the "asort" function? A built-in function?
I understand the "num_desc" to sort numeric values in descending order, but what about the "@val_"?

Last edited by Trd300; 09-26-2012 at 07:39 PM.
 
Old 09-27-2012, 06:34 AM   #50
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Quote:
* Is "temp" a built-in variable? (it is not define anywhere, so I assume yes) What does it refer to? Only the lines without "@" at the beginning?
No, temp is just a variable name I chose only need to use it once. It is storing the next line, ie. 12345678
Quote:
* What is the third parameter in the "asort" function? A built-in function?
I understand the "num_desc" to sort numeric values in descending order, but what about the "@val_"?
Third parameter is the "how" to sort the array:

val - look at values not indexes
num - compare as numbers not strings
desc - descending order

The reference below should help explain further:

http://www.gnu.org/software/gawk/man...lling-Scanning
 
1 members found this post helpful.
Old 09-27-2012, 07:08 AM   #51
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
That gawk manual is pretty much complete already when it comes to information and references. I got most that I needed from it when I made a compiler for a project.

Try to master it and you'll see a big difference to your Awk scripting capabilities.
 
Old 09-27-2012, 06:57 PM   #52
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Thanks grail & konsolebox for your help and explanations !
 
  


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
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 8 07-13-2015 02:54 AM
[awk script] Help me delete lines in a file using script ? sieukid Programming 5 03-20-2012 02:23 PM
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 08:00 PM
Using file content as input for awk search patterns srn Programming 2 09-13-2011 03:49 AM
Plz tell me, how to get input in awk script intikhabalam Linux - General 1 07-27-2008 08:01 AM


All times are GMT -5. The time now is 08:18 AM.

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