LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk variable troubles (https://www.linuxquestions.org/questions/linux-newbie-8/awk-variable-troubles-4175462755/)

Kustom42 05-20-2013 05:33 PM

awk variable troubles
 
Hi all,

Still working on getting better with awk, tried to use some of the advice members had given on other threads related to this but am having trouble figuring out where I'm going wrong.

Here's the code:
I'll spare you from reading most of the code as I do know for a fact that my variable is being set correctly to an integer when I run it with sh -x.

So currently my sh -x shows the LASTOCTET benig set as:

LASTOCTET=64

Then this code is a few lines down:
Code:

        if [[ "$LASTOCTET" -lt 254 ]]
        then
        LASTOCTET=$(($LASTOCTET + 1))
        TMPIP=$(echo $LASTIP | awk -v "n=$LASTOCTET" -F "." '{print $1"."$2"."$3"."$n }')
        echo -e "$VM\t$TMPIP" >> /tmp/test1
        elif [[ "$LASTOCTET" -ge 255 ]]
        then
        LASTOCTET=0
        TMPIP=$(echo $LASTIP | awk -v "n=$LASTOCTET" -F "." '{ print $1"."$2+1"."$3"."$n }' )
        echo -e "$VM\t$TMPIP" >> /tmp/test1
        fi

Here is more of the sh -x output showing what is going on:

Code:

+ LASTOCTET=65
++ echo 10.13.2.64
++ awk -v n=65 -F . '{print $1"."$2"."$3"."$n }'
+ TMPIP=10.13.2.
+ echo -e 'TEST1\t10.13.2.'


What am I doing wrong here? It looks like im passing the varible into AWK correctly as I can see it being set to 65 so why is the $n not being printed?

jlinkels 05-20-2013 09:10 PM

Awk's field variables are $1, $2 etc. Regular variables are referenced wthout '$'.

I have also become a fan of printing with printf. It shouldn't make much difference but formatting is easier: printf ("%d.%d.%d.%d\n", $1, $2, $3, n)

$n happens to be $1, $2,... for n=1, 2... Incredible flexible language! Hence most likely you were referring to $65 which doesn't exist.

jlinkels

grail 05-21-2013 09:10 AM

First off, in the bash portion of your script you use round brackets some times and other times square. As you are doing numerical comparisons I would suggest using round all the time to save the
confusion.

Secondly, do you really need awk here??

So without seeing the rest and assuming data correct, it could look like:
Code:

        if (( LASTOCTET < 254 ))
        then
            (( LASTOCTET++ ))
            TMPIP="${LASTIP%.*}.$LASTOCTET"
            echo -e "$VM\t$TMPIP" >> /tmp/test1
        elif (( LASTOCTET >= 255 ))
        then
            TMPIP="${LASTIP%.*}.0"
            echo -e "$VM\t$TMPIP" >> /tmp/test1
        fi


Kustom42 05-21-2013 12:26 PM

Quote:

Originally Posted by grail (Post 4955753)
First off, in the bash portion of your script you use round brackets some times and other times square. As you are doing numerical comparisons I would suggest using round all the time to save the
confusion.

Secondly, do you really need awk here??

So without seeing the rest and assuming data correct, it could look like:
Code:

        if (( LASTOCTET < 254 ))
        then
            (( LASTOCTET++ ))
            TMPIP="${LASTIP%.*}.$LASTOCTET"
            echo -e "$VM\t$TMPIP" >> /tmp/test1
        elif (( LASTOCTET >= 255 ))
        then
            TMPIP="${LASTIP%.*}.0"
            echo -e "$VM\t$TMPIP" >> /tmp/test1
        fi



I tried using round brackets but was having issues with the integer having a space appended to it so instead of worrying too much about it changed it to square to do a string comparison. I switched to using awk because I was going to have to manipulate the different octets of teh IP easily and I figured awk could split it and rejoin the IP easy enough. If it was just the last octet it would make it much easier.

grail 05-21-2013 01:11 PM

Quote:

I tried using round brackets but was having issues with the integer having a space appended to it so instead of worrying too much about it changed it to square to do a string comparison.
This doesn't really make much sense :(
Code:

if [[ "$LASTOCTET" -lt 254 ]]
By using -lt you are doing a numeric comparison and not a string comparison: (below is from man test)
Code:

INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

Quote:

I switched to using awk because I was going to have to manipulate the different octets of teh IP easily and I figured awk could split it and rejoin the IP easy enough.
So if it is manipulating octects you wish to do, bash is still very capable:
Code:

OCTETS=(${LASTIP//./ })
Now you can simply call each element of the OCTETS array for whichever piece you wish :)

David the H. 05-21-2013 01:28 PM

I know I've already suggested splitting the octets into an array in one of the previous threads, even including code for it.

Kustom42 05-21-2013 01:28 PM

Quote:

Originally Posted by grail (Post 4955899)
This doesn't really make much sense :(
Code:

if [[ "$LASTOCTET" -lt 254 ]]
By using -lt you are doing a numeric comparison and not a string comparison: (below is from man test)
Code:

INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2


I'm not sure why but I was receiving an invalid string error which showed an additional space being appended to the integer it was comparing to when using round brackets. I don't usually have much time to figure out why on most of these things, trying to administrate an extremely large environment by myself and since I transitioned from being a Linux admin to a backup admin I basically stopped scripting and now am re-learning a lot of what I used to know a year or two ago.

Thanks for the input and knowledge grail.

Kustom42 05-21-2013 01:29 PM

Quote:

Originally Posted by David the H. (Post 4955911)
I know I've already suggested splitting the octets into an array in one of the previous threads, even including code for it.

Yes you have, I attempted to put in place the code you gave but I am also scripting against a proprietary database that is absolutely nothing like any sort of SQL and its formatting its garbage. I wasn't able to get it to work 100% correctly.

David the H. 05-21-2013 01:33 PM

Like grail though, it doesn't make sense to me that the brackets used would be the cause for something like that. The extra space had to be added elsewhere, and was probably sitting there from before the test, perhaps even carried into it from the original input?


All times are GMT -5. The time now is 04:55 PM.