apply a function twice successively with the same input in awk program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers apply a function twice successively with the same input in awk program
# 8  
Old 09-19-2012
OK, i try with an example.

This thread can be related with this one:
https://www.unix.com/unix-dummies-que...#post302702771

Let's say we have a one-record input like that:
Code:
abcdef

I would like to convert to numbers blocks of 2 letters (for which I defined a function) by starting from the first letter, than the second, then the third, then the fourth, etc

conversion table:
Code:
conv["ab"]="1"; conv["bc"]="2"; conv["cd"]="3"; conv["de"]="4"; conv["ef"]="5"

to get this output:
for i=1 (starting from 1st letter)
Code:
135

for i=2 (starting from 2nd letter)
Code:
24X

etc.

But every time I modify the value of "i" I have to use the original input (not the output from the previous call)
Code:
BEGIN {conv["ab"]="1"; conv["bc"]="2"; conv["cd"]="3"; conv["de"]="4"; conv["ef"]="5"}
    
function convert(i)   
    {
        numb = ""
        do{
            letter = substr($1, i, 2)
            numb = numb (letter in conv ? conv[letter] : "X")
            i=i+2}
        while(letter!="")
        return $1=numb
    }

    { print convert(1) }     # use original input
    { print convert(2) }     # use also original input
    etc.


I hope it helps you to see my point !
# 9  
Old 09-19-2012
So you want the value of i to keep increasing with each call?

Don't make it a parameter then, and it will be a global variable.
# 10  
Old 09-19-2012
Quote:
So you want the value of i to keep increasing with each call?
Yes, until a certain limit though.

Quote:
Don't make it a parameter then, and it will be a global variable.
So what you are suggesting is that I shouldn't mention the "i" parameter in the function, and use a for loop to assign a value to "i" instead?

something like that maybe:
Code:
BEGIN {conv["ab"]="1"; conv["bc"]="2"; conv["cd"]="3"; conv["de"]="4"; conv["ef"]="5"}
    
function convert  (letter) 
    {
        numb = ""
        do{
            letter = substr($1, 1, 2)
            numb = numb (letter in conv ? conv[letter] : "X")
            }
        while(letter!="")
        return $1=numb
    }

    { for (i=1; i<=5; i++){
           letter=substr($1,i,2)
           convert(letter)
           }
    }1

but it will only consider the first block...
# 11  
Old 09-19-2012
I'm saying to not use a loop for it at all. Just increment it in the function itself.
# 12  
Old 09-20-2012
How can you increment in the function itself without a for loop????

The only way I see how to do that is like:
Code:
BEGIN {conv["ab"]="1"; conv["bc"]="2"; conv["cd"]="3"; conv["de"]="4"; conv["ef"]="5"}


function convert(letter){
        numb = ""
        for(start=1; start<=5; start++){
            ss = substr(letter, start, 2)
            while( ss != "" ){
               numb = numb (ss in conv ? conv[ss] : "X")
               start = start + 2
               ss = substr(letter, start, 2)
           }
        }
        return numb
    }

{ $1 = convert($1);
   print $1}


Last edited by beca123456; 09-21-2012 at 06:40 AM..
# 13  
Old 09-21-2012
Stop bumping your posts. It doesn't make answers faster.

i=i+1?

If that won't do, then I don't understand your problem.
# 14  
Old 09-22-2012
Sorry!

For each line of the input, I need to convert the string starting from the first letter, then starting from the second letter, then the third, etc.

input:
Code:
abcdef

ouput:
Code:
135   # starting from the first letter (i.e. "abcdef")
24X   # starting from the second letter (i.e. "bcdef")
35     # starting from the third letter (i.e. "cdef")
4X     # starting from the fourth letter (i.e. "def")
5       # starting from the fifth letter (i.e. "ef")

I tried:
Code:
BEGIN {conv["ab"]="1"; conv["bc"]="2"; conv["cd"]="3"; conv["de"]="4"; conv["ef"]="5"}


function convert(letter){
        for(i=1; i<=5; i++){
                numbi = ""
                l = length(letter)
                stringi = substr(letter, i, (l-i)+1)        
                ss = substr(stringi, start, 2)
                while(ss != ""){
                        numbi = numbi (ss in conv ? conv[ss] : "X")
                        start = start + 2
                        ss = substr(stringi, start, 2)
                }
        return numbi
        }
}

{ $1 = convert($1);
   print $1}


But it returns only the first increment (and it is wrong).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk program date function no longer running

I work at a company that uses a program written in AWK to track various data and prepare reports. Worked with this program for three years plus (the author is no longer with us) and the YTD Production report will not return a report with a date after 123119. This is a problem. Below is the (I... (3 Replies)
Discussion started by: paulgdavitt
3 Replies

2. Shell Programming and Scripting

awk to match and apply condtions to matchijng files in directories

I am trying to merge the below awk, which compares two files looking for a match in $2 and then prints the line if two conditions are meet. awk awk 'FNR==NR{A=$0;next} ($2 in A){if($10>30 && $11>49){print A}}' F113.txt F113_tvc.bed This code was improved and provided by @RavinderSingh13,... (18 Replies)
Discussion started by: cmccabe
18 Replies

3. Shell Programming and Scripting

OFS does not apply to few records in awk

Hi , I am having a problem with my awk oneliner , which for some reason leaves the first two records Input File $ cat file1 A1:B1:C1:NoLimit M1:M2:M3:Limit A2:B2:C2,C3,C4,C5 A3:B3:C3,C4,C5,C6,C7Desired output A1,B1,C1,NoLimit M1,M2,M3,Limit A2,B2,C2 ,,,C3 ,,,C4 ,,,C5 A3,B3,C3... (5 Replies)
Discussion started by: chidori
5 Replies

4. UNIX for Dummies Questions & Answers

Run executable in one directory and then move to another successively

Hello, I have several hundred subdirectories which contain input files for a binary executable. I need to get into each of the subdirectories, run the executable and then move to the next one and repeat the process. What is the best way to do this? Arbitrarily my file structures look like... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

5. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

6. Shell Programming and Scripting

apply record separator to multiple files within a directory using awk

Hi, I have a bunch of records within a directory where each one has this form: (example file1) 1 2 50 90 80 90 43512 98 0909 79869 -9 7878 33222 8787 9090 89898 7878 8989 7878 6767 89 89 78676 9898 000 7878 5656 5454 5454 and i want for all of these files to be... (3 Replies)
Discussion started by: amarn
3 Replies

7. Shell Programming and Scripting

How to apply sub/gsub of awk on a particular field?

I got a file with contents are of the following format ... 2007-09-28 ./.passwwd1.sh.swp 2007-11-26 ./827-55.jpg 2007-09-28 ./argcheck.pl ... I have to delete all the '-' in the "first field", ie. the date, not on the second field, which is the name of the file. i.e. required output ... (1 Reply)
Discussion started by: jaduks
1 Replies

8. UNIX for Dummies Questions & Answers

Apply Regex to input read

Hi I am very new to Unix Shell. I have a question: How do you check the input value against a reqex? For example using korn Shell: echo "Please enter revision month: \c" read revmon if $revmon | egrep -c = '{3}{2}"' then echo "OK" else echo "PLease re-enter a valid revision... (3 Replies)
Discussion started by: bonekrusher
3 Replies

9. UNIX for Dummies Questions & Answers

How to apply the awk commnad ?

Hi, I have a file and the contents of the file is say World World World Now i need to append some more words in each of the line and the the output of the file should like the one below Will India win the World Cup? Will India win the World Cup? Will India win the... (3 Replies)
Discussion started by: preethgideon
3 Replies

10. Shell Programming and Scripting

awk - input function

hello, im a newbie. how can i input data from keyboard? thanks. (3 Replies)
Discussion started by: Jariya
3 Replies
Login or Register to Ask a Question