Sponsored Content
Full Discussion: Passing variables into AWK
Top Forums Shell Programming and Scripting Passing variables into AWK Post 302713113 by Boomn4x4 on Wednesday 10th of October 2012 09:46:01 AM
Old 10-10-2012
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

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies

2. Shell Programming and Scripting

Passing Variables to Awk

Hi I have a unix shell script with an awk statement. I would like to print some of the fields of an input file. However, I would like to print them dynamically, ie by passing the literal $1 $3 into the script to define the output. I have tried the following: variable1='$1' awk... (2 Replies)
Discussion started by: Bab00shka
2 Replies

3. Shell Programming and Scripting

Passing awk Variables

I am trying to pass the results from a variable gathered from awk, however when I echo the 'PARSE' and 'SUB', the response is blank. This is my command. awk -F= '/Unit/''{ PARSE=substr($2,1,5) ; SUB=substr($2,1,1) }' inputfile.lst Is this a kind of valid attempt or am I obligated to declare... (3 Replies)
Discussion started by: gozer13
3 Replies

4. Shell Programming and Scripting

Passing space seprated variables in awk

Hi, How to pass space seprated variables in awk. (HP-UX, sh shell) I have the problem like below: var="Hello" var2="Manu Batham" echo $var2 | awk -v variable=${var} '{ printf "%s %s", variable, $1}' and its output is: Hello Manu while output should be: Hello Manu Batham Please... (4 Replies)
Discussion started by: manubatham20
4 Replies

5. Shell Programming and Scripting

Passing awk variables to shell

Hi. I need to parse file and assign some values to variables, right now i do like below MYHOMEDIR=`awk '/Home/ {print $NF}' output.txt` MYSHELL=`awk '/Shell/ {print $NF}' output.txt` PRGRP=`awk '/Primary/ {print $NF}' output.txt` SECGRP=`awk '/Second/ {print $NF}' output.txt` In this... (10 Replies)
Discussion started by: urello
10 Replies

6. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

7. Shell Programming and Scripting

PAssing variables to awk arithmetic

Hi all, I am wanting to pass variables from a file to an awk arithmetic formula. When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

8. Shell Programming and Scripting

Passing variables to awk

Hi guys, I need to fetch data from logfile between two given dates,i got the below code from our forum.It works perfect,but i need to enter the value dynamically to awk while running. awk '/2012 Jun/{p=1}!/2012 Jul/ && prev~/2012 Jul/ && p{p=0}{prev=$0}p' file i tried the below code,but... (4 Replies)
Discussion started by: mohanalakshmi
4 Replies

9. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

10. Shell Programming and Scripting

awk - passing variables in and out

Am looking to pass some Linux environment variables into AWK , can I simply use the -v option ? awk -F: -v AHOME=$HOME '{ if {rm AHOME/file.txt a=2 } }' config.txt ... (4 Replies)
Discussion started by: alldbest
4 Replies
POTOOL(1)						      General Commands Manual							 POTOOL(1)

NAME
potool - program for manipulating gettext po files SYNOPSIS
potool FILENAME1 [ FILENAME2 ] [-f f|nf|t|nt|nth|o|no] [-n ctxt|id|str|cmt|ucmt|pcmt|scmt|dcmt|tr|linf]... [-s] [-c] potool -h DESCRIPTION
potool works in two (so far) modes. The first mode requires providing one file name, and works as a filter. In the second mode, the program replaces the translations in FILENAME1 with the translations from FILENAME2. (So FILENAME1 is the base po file, while FILENAME2 is our working copy.) OPTIONS
-f filter Determines which po file entries should be retained. In the second mode, the filters are applied only to FILENAME2 (the working copy). Existing filters are: t - translated entries nt - untranslated entries nth - untranslated entries and the header f - fuzzy entries nf - entries that are not fuzzy o - obsolete entries no - non-obsolete entries It is possible to stack filters, by specifying multiple -f options. -n filter Determines which po file entries parts should not be retained. Any number of -n options is allowed. Valid parameters are: ctxt - don't write 'ctxt' parts id - don't write 'id' parts str - don't write 'str' parts tr - don't write translations ucmt - don't write user's comments pcmt - don't write the comments regarding position in source files scmt - don't write special comments ('#, fuzzy, c-format, ...') dcmt - don't write reserved comments (usually starting with a dot) cmt - don't write any comments linf - change source line numbers to '1'. The last parameter is useful when you need to compare two po or pot files using diff(1) as it usually returns lots of unimportant line number changes otherwise. -s Don't display the entries themselves, only their count. -c Overwrite all msgstrs with their msgids. -h Display short usage help. EXAMPLES
potool x.po -s -ft displays the number of translated entries. See also postats(1). potool x.po -nstr Deletes all translations - so you can start from scratch! :-) potool x.po -ft && potool x.po -fnt displays firstly the translated and then the non-translated entries from file x.po (reverse order is not recommended because of the first "header" entry). The output contains all information from x.po, with the difference that untranslated entries are located together in a single place. potool x.po -fnt > tmp.po && editor tmp.po && potool x.po tmp.po lets you easily add new translations, without looking at the already translated entries The last two examples are implemented as the potooledit(1) program. SEE ALSO
potooledit(1), postats(1), msgmerge(1), msgfmt(1). AUTHOR
Potool was written by Zbigniew Chyla and is now being maintained by Marcin Owsiany <porridge@debian.org>. September 21, 2007 POTOOL(1)
All times are GMT -4. The time now is 01:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy