Link to home
Create AccountLog in
Avatar of Phonebuff
PhonebuffFlag for United States of America

asked on

Awk Sub() function with arary indexs

Good Evening,

    Need some help with a basic awk script..

    I have created to arrays and wish to replace the value in element n of array 1 with the Value of Element n in array 2.

Awk '
    BEGIN {

               split("value1 value2 valuen", array_1, " ")
               split("newval1 newval2 newvaln", array_2, " ")
               }

                    for (x=1, x<=15, x++) {
                              if (array_1[x] == $6) break;
                     }
                    sub(/array_1[x]/, array_2[x] );  print
 '

     But the output string is not altered.
           
      ============
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Hi, I don't exactly understand what you goal is but the sub command is not working the way you think is. How about this:

awk '
BEGIN {
       	split("value1 value2 valuen", array_1, " ")
       	split("newval1 newval2 newvaln", array_2, " ")

	for ( x in array_1 ) {
		print array_1[x]
	}

	for ( x in array_2 ) {
		print array_2[x]
	}

       	for (x=1;x<=15;x++) {
       		if (array_1[x] == $6) break;
               		array_1[x]=array_2[x]
       	}

	for ( x in array_1 ) {
		print array_1[x]
	}

	for ( x in array_2 ) {
		print array_2[x]
	}
}
'

Open in new window


This will split the 2 strings into 2 arrays and print them, then in a loop assign values of one array to the other and print again. What is your if supposed to to? The way it's now it's doing nothing...
Avatar of Phonebuff

ASKER

Thanks for the quick response I think I found the answer, although as clean as I hopped.

grep tun /var/log/messages |
awk '
BEGIN {
       	split("value1 value2 valuen", array_1, " ")
       	split("newval1 newval2 newvaln", array_2, " ")
        }

       {
       	  for (x=1;x<=15;x++) {
       		if (array_1[x] == $6) break;
       	  }

          sub(/tun[0-9][ 0-9]/, array_2[x]) print

        }
'

Open in new window


    Not sure why I can't use array_1[x] as the value in parameter 1.
Well, your post now looks different that the original one... What do you want to accomplish exactly?
The basic  task is to replace a string in $0 with a string from array_2.

An yes the new sub() in my response does work, I just don't understand why the array_1[x] can not be in the first variable of the sub() statement.  Or if it can how to code it.

==============
This is because the first parameter of sub needs to be a regular expression so:

/<expr>/

In your case, the regular expression is array_1[x] which matches these strings:

array_1 or array_1x

.. but notthe contents of your array, get it?
Sorry,  I am still not able to make this work..

I tried many variations on the theme with no success till I just dropped the array...

sub(/array_1[x]/, array_2[x], $0)
sub(/$array_1[x]/, arrary_2[x], $0)
sub ("/arrary_1[x]"/, array_2[x] )
excreta ---

Only the hard code of sub(/tun[0-9][ 0-9]/, array_2[x]) seemed to work.

---------------------
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you ---