Avatar of Phonebuff
Phonebuff
Flag 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.
           
      ============
Linux OS DevSoftware

Avatar of undefined
Last Comment
Phonebuff

8/22/2022 - Mon
Gerwin Jansen

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...
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.
Gerwin Jansen

Well, your post now looks different that the original one... What do you want to accomplish exactly?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Phonebuff

ASKER
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.

==============
Gerwin Jansen

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?
Phonebuff

ASKER
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.

---------------------
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Gerwin Jansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Phonebuff

ASKER
Thank you ---