Passing Global Shell variables to awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing Global Shell variables to awk
# 1  
Old 08-26-2013
Passing Global Shell variables to awk

Hi All,

Iam trying to pass global shell variables and is not working

Main script is like below

Code:
CYEAR=`date +"%y"`
CFYEAR=`date +"%Y"`
CMONTH=`date +"%m"`

if [ $CMONTH -eq "01" ]
then
        PMONTH=12
        PYEAR=`expr $CYEAR - 1`
        PFYEAR=`expr $CFYEAR - 1`
else
        PMONTH=`expr $CMONTH - 1`
        if [ $PMONTH -lt 10 ]
        then
                PMONTH=0$PMONTH
        fi
        PYEAR=$CYEAR
        PFYEAR=$CFYEAR
fi

echo $CMONTH
echo $CYEAR
echo $CFYEAR
echo $PMONTH
echo $PYEAR
echo $PFYEAR

export CMONTH CYEAR CFYEAR PMONTH PYEAR PFYEAR

get_user_history.sh


The get_user_history.sh is like below

Code:
> /dummy/history/history.$PMONTH.$PFYEAR
> /dummy/history/history.$CMONTH.$CFYEAR.copy
awk -v CMONTH=$CMONTH -v CYEAR=$CYEAR -v CFYEAR=$CFYEAR -v PMONTH=$PMONTH -v PYEAR=$PYEAR -v PFYEAR=$PFYEAR -f /dummy/scripts/prepare_history_files.sh   /dummy/backup_history/history.$CMONTH.$CFYEAR.copy


prepare_history_files.sh is like below

Code:
BEGIN { FS = "|"}

{ if (($5 == "$PMONTH") && ($6 == "$PYEAR")) print $0 >> "/dummy/history/history.$PMONTH.$PFYEAR";
}
{ if (($5 == "$CMONTH") && ($6 == "$CYEAR")) print $0 >> "/dummy/history/history.$CMONTH.$CFYEAR.copy";
}


Where am I going wrong?
# 2  
Old 08-26-2013
You would access the awk variables as such:
Code:
if (($5 == PMONTH) ... ) ...

etc.

i.e.
Code:
$ X=1            
$ echo 1 | awk -v X=$X '{ if( $1 == X ) print "Yes" }'
Yes
$ echo 2 | awk -v X=$X '{ if( $1 == X ) print "Yes" }'
$

# 3  
Old 08-26-2013
working in if but not in file name

It working in the if condition like below

Code:
{ if (($5 == PMONTH) && ($6 == PYEAR)) print $0


but not in the file name like below

Code:
"/dummy/history/history.$PMONTH.$PFYEAR";

Code:
"/dummy/history/history.$CMONTH.$CFYEAR.copy";

# 4  
Old 08-26-2013
You need to take the variables out of the string (and not reference them with $), and concatenate the string you want:
Code:
"/dummy/history/history." PMONTH "." PFYEAR

This User Gave Thanks to Scott For This Post:
# 5  
Old 08-26-2013
If they didn't work with $ the first time, they won't work with $ the second, third, or fourth time. $ has a different meaning in awk anyway, it means column. Smilie

awk doesn't substitute variables inside strings, they have to be outside. You can concatenate strings and variables just by listing them in order though, so almost as easy.

Try this, with the variables outside double quotes:

Code:
"/dummy/history/history." PMONTH "." PFYEAR;


Last edited by Corona688; 08-26-2013 at 01:22 PM..
# 6  
Old 08-26-2013
... >> ("/dummy/history/history." PMONTH "." PFYEAR);
This User Gave Thanks to neutronscott For This Post:
# 7  
Old 08-26-2013
Corona688 you could be little gentler in your replies...

I did remove the $ and still it did not work so I kept it back

now I think I will try the new way....thanks all to your replies...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: passing shell variables through and extracting text

Hello, new to the forums and to awk. Glad to be here. :o I want to pass two shell (#!/bin/sh) variables through to awk and use them. They will determine where to start and stop text extraction. The code with the variables hard-coded in awk works fine; the same code, but with the shell... (7 Replies)
Discussion started by: bedtime
7 Replies

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

3. Shell Programming and Scripting

Problem in Global variables in shell script

hi, i have a shell script which calls another shell which in turn calls another shell script. Main_shell_script.sh echo "In Main_shell_script.sh" FILENAME="original.txt" # LINE 1 DST_FILENAME=$FILENAME # LINE 2 echo "FILENAME = {$FILENAME}" echo "DST_FILENAME =... (3 Replies)
Discussion started by: Little
3 Replies

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

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. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. Solaris

How to access ENV variables of non global zones in global zone???

Hi Guys, My requirement is I have file called /opt/orahome/.profile in non global zone. PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:. export PATH PS1="\${ORACLE_SID}:`hostname`:\$PWD$ " export PS1 EDITOR=vi export EDITOR ENV=/opt/orahome/.kshrc export ENV... (1 Reply)
Discussion started by: vijaysachin
1 Replies

8. UNIX for Dummies Questions & Answers

How to declare global variables for shell script

Hi, I do have some variables accessed in more than one script. I want to have those variables in a command file anduse them. Something like a header file that we use in C programs. I dont want to set them as environment variables. Is there any other option, like header file in scripting ?? ... (2 Replies)
Discussion started by: risshanth
2 Replies

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

10. Shell Programming and Scripting

Passing shell variables to awk program..

Hello, Can we pass shell variables like $PATH etc. to a awk program part for example, awk ' { fieldValue=$PATH .... }' file (1 Reply)
Discussion started by: Vishnu
1 Replies
Login or Register to Ask a Question