LinuxQuestions.org
Help answer threads with 0 replies.
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 05-20-2013, 06:33 PM   #1
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
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?
 
Old 05-20-2013, 10:10 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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

Last edited by jlinkels; 05-21-2013 at 10:15 AM.
 
1 members found this post helpful.
Old 05-21-2013, 10:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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
 
Old 05-21-2013, 01:26 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
Quote:
Originally Posted by grail View Post
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.
 
Old 05-21-2013, 02:11 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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
 
1 members found this post helpful.
Old 05-21-2013, 02:28 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I know I've already suggested splitting the octets into an array in one of the previous threads, even including code for it.
 
1 members found this post helpful.
Old 05-21-2013, 02:28 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
Quote:
Originally Posted by grail View Post
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.
 
Old 05-21-2013, 02:29 PM   #8
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
Quote:
Originally Posted by David the H. View Post
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.
 
Old 05-21-2013, 02:33 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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?
 
  


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
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
/bin/sh awk command troubles PatrickNew Programming 13 08-19-2007 02:55 PM
awk on a variable onradius Programming 8 02-22-2006 09:34 AM


All times are GMT -5. The time now is 01:29 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