Passing variables into AWK | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing variables into AWK

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-10-2012
Boomn4x4 Boomn4x4 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 17 January 2013, 8:21 AM EST
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working.


Code:
awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2'   BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; }

    /mqhost1/   { mqhost1=1; }
     /mqhost2/    { mqhost2=1; }
       /myip1/    { stap1=1;   }
    /myip2/    { stap2=1;   }
                    { print $0; }
        END     { 
                  if (mqhost1 == 0)
                      printf("myip1          mqhost1.test.com       mqhost1      \n");
                  if (mqhost2 == 0)
                                      printf("myip2          mqhost2.test.com       mqhost2      \n");
                  if (stap1 == 0)
                      printf("$IP1          $HOST1.test.com   $HOST1  \n");
                  if (stap2 == 0)
                                      printf("$IP2          $HOST2.test.com   $HOST2  \n");

                  }' /client_root/etc/hosts > /home/me/hosts

Sponsored Links
    #2  
Old Unix and Linux 10-10-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
You need -v for each variable..Unix or Linux Image


Code:
awk -v myip1=$IP1 -v myip2=$IP2 -v myhost1=$HOST1 -v myhost2=$HOST2

and you can try for variable search like...


Code:
$0 ~ myip2 { }


Last edited by pamu; 10-10-2012 at 09:53 AM..
Sponsored Links
    #3  
Old Unix and Linux 10-10-2012
Boomn4x4 Boomn4x4 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 17 January 2013, 8:21 AM EST
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks, that saved some trouble, however, my output is putting out literal text, not the variable value. My output:

myip1 myhost1.test.com myhost1
myip2 myhost2.test.com myhost2
myip1 myhost1.test.com myhost1
myip2 myhost2.test.com host2
    #4  
Old Unix and Linux 10-10-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by Boomn4x4 View Post
Thanks, that saved some trouble, however, my output is putting out literal text, not the variable value. My output:

myip1 myhost1.test.com myhost1
myip2 myhost2.test.com myhost2
myip1 myhost1.test.com myhost1
myip2 myhost2.test.com host2
Remove double quotes and you don't need printf also.. In double quotes it is considered as a string..Unix or Linux Image


Code:
if ( ) {print myip1, mqhost1".test.com", mqhost1}


Last edited by pamu; 10-10-2012 at 10:37 AM.. Reason: corrected..
The Following User Says Thank You to pamu For This Useful Post:
Boomn4x4 (10-10-2012)
Sponsored Links
    #5  
Old Unix and Linux 10-10-2012
Boomn4x4 Boomn4x4 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 17 January 2013, 8:21 AM EST
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
The print statements failed with the "." and the "/n" so I had to put the "." in quote and put some other quotes in to accomodate spacing and the /n got removed... but it works

Thank you.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing 2 variables tvamsikiran Shell Programming and Scripting 3 04-03-2010 05:44 AM
passing variables to sed in ksh d.anggrianto Shell Programming and Scripting 2 06-04-2009 02:36 PM
Passing variables to sed rogers42 Shell Programming and Scripting 3 10-29-2007 02:42 PM
Passing variables to sed jfisch Shell Programming and Scripting 3 03-07-2005 06:25 AM
Passing Variables to AWK AreaMan Shell Programming and Scripting 5 01-28-2002 05:30 PM



All times are GMT -4. The time now is 10:19 PM.