Initialize Variable Using awk Expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Initialize Variable Using awk Expression
# 1  
Old 06-10-2013
Initialize Variable Using awk Expression

Hello All,
What i am trying to do is read the input file delimited by pipe and do further logic like simulating the for loop but using awk not sure if this a right approach but as i was told doing "for i in `cat $file`" is an over kill so trying to replicate the same thing using awk script below. I am using bash shell and in the below awk expression i have 2 questions

1) Why print $1 is working but not echo $1?
2) How do i initialize or assign a value to the variable UNINUM like UNINUM=`print $1` or UNINUM=$1?

Code:
awk -F\| '
{
#       print "Provider: " $1, " Uninum: " $2, " Extract Date: " $3, " Loading Status: " $4
echo $1
print $1
#UNINUM=$1
echo $UNINUM
}
' "dwh_StageLoadEmail.lst"

dwh_StageLoadEmail.lst
Code:
710119|16|2012-09-30|COMPLETED
610000|16|2012-09-30|COMPLETED
710056|16|2012-09-30|COMPLETED

# 2  
Old 06-10-2013
The reason is because echo is not a valid awk statement but print is.

You can assign a shell variable to awk variable using -v option:
Code:
awk -v UNINUM="$1"

For further reference check Assignment Options
# 3  
Old 06-10-2013
ok i think i am doing wrong, i have a for loop in my shell script which reads a file as below, i am trying to implement the same logic using awk that is what i am trying to achive above in my awk script, is that possible !? may be not as awk,sed are extensions to shell just to format output,file,custom print etc..but shell is the one where you do your programming looping etc..please correct me if i am wrong!

Code:
for i in `cat "${GEN_PARAM_LIST1}"`
        do
        UNINUM=`echo $i | awk -F"|" '{print $1}'`
        PROVIDER=`echo $i | awk -F"|" '{print $2}'`
        EXTRACT_DT=`echo $i | awk -F"|" '{print $3}'`
        VER_NUM=`echo $i | awk -F"|" '{print $4}'`

sqlplus ......etc
done

# 4  
Old 06-10-2013
The approach that you are using is not recommended. Also using awk inside a shell for loop is an overkill.

Set the IFS to pipe | and read each fields into different variables using a shell while loop:
Code:
while IFS="|" read UNINUM PROVIDER EXTRACT_DT VER_NUM
do
        # action here
done < "${GEN_PARAM_LIST1}"

# 5  
Old 06-11-2013
Hello Yoda,
I am trying to assign a value to the awk variable but getting syntax error, i read the links you provided but don't find a proper example. Can you please help, thank you.

Code:
awk -F\| '
awk -v LOCATION=/var/tmp -v DATAFILE=xxxx.xml -v ARG1=$1
BEGIN {
print "Informatica Was Able To Successfully Load the XML Data File $LOCATION/$DATAFILE"
}
{
print "Provider: " $1, " Uninum: " $2, " Extract Date: " $3, " Loading Status: " $4
}
' OFS=, "/home/infrmtca/bin/log/dwh_StageLoadEmail.lst" | /usr/bin/mutt -s "Provider XML Data File Loaded Successfully" "${EMAIL_LIST}"

# 6  
Old 06-11-2013
Below lines should be replaced
Code:
awk -F\| '
awk -v LOCATION=/var/tmp -v DATAFILE=xxxx.xml -v ARG1=$1

with
Code:
awk -F\| -v LOCATION="/var/tmp" -v DATAFILE="xxxx.xml" -v ARG1="$1" '

This User Gave Thanks to Yoda For This Post:
# 7  
Old 06-11-2013
Thank you i replaced it but i don't see those variables expanded.

Code:
awk -F\| -v LOCATION="/var/tmp" -v DATAFILE="xxxx.xml" '
BEGIN {
print "Informatica Was Able To Successfully Load the XML Data File" $LOCATION, $DATAFILE
}
{
print "Provider: " $1, " Uninum: " $2, " Extract Date: " $3, " Loading Status: " $4
}
' OFS=, "/home/infrmtca/bin/log/dwh_StageLoadEmail.lst" | /usr/bin/mutt -s "Provider XML Data File Loaded Successfully" "${EMAIL_LIST}"

Output:
Code:
Informatica Was Able To Successfully Load the XML Data File 
Provider: 925000, Uninum: 6, Extract Date: 2012-12-31, Loading Status: 7

---------- Post updated at 10:13 AM ---------- Previous update was at 10:04 AM ----------

Its working when i removed the $ symbol for those awk variables in the print statement. thank you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

2. Shell Programming and Scripting

initialize a variable only once

hi, i have a script which needs to be run every 10 minutes.This is achieved using crontab utility, it is checking Number of calls on a service... if number of calls are not increasing then it will stop the service else do nothing. Now in the script, i fetch the current value in variable... (2 Replies)
Discussion started by: gauravah
2 Replies

3. Shell Programming and Scripting

Variable expression and while

hi, i'm reading a file "LISTE_FILE" like : # $LOGCOM * 5 $PRCCOM * 10 and i want to use the file with "while" and having the fields splitted into new variables for treatment : while read LINE do # Ignorer les commentaires un # en premiere position if then... (3 Replies)
Discussion started by: Nicol
3 Replies

4. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

5. Shell Programming and Scripting

Initialize a variable

Hi All, Please can you advise on how can I initialize a variable with a line. Suppose i want to initialize x with line redirects.www.virginatlantic.com.conf Best Regards, Shazin (1 Reply)
Discussion started by: Shazin
1 Replies

6. Shell Programming and Scripting

AWK - compare $0 to regular expression + variable

Hi, I have this script: awk -v va=45 '$0~va{print}' flo2 That returns: "4526745 1234 " (this is the only line of the file "flo2". However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That... (3 Replies)
Discussion started by: jolecanard
3 Replies

7. Shell Programming and Scripting

Awk's variable in regular expression

Anyone know how I will use awk's variable in a regular expression? This line of code of mine is working, the value PREMS should be a variable: awk '$1 ~ /PREMS/ { if(length(appldata)+2 >= length($1)) print $0; }' appldata=$APPLDATA /tmp/file.tmp The value of APPLDATA variable is PREMS. ... (2 Replies)
Discussion started by: Orbix
2 Replies

8. Shell Programming and Scripting

Awk - Using a Shell Variable in the Reg Expression

Hi all, I have a shell variable $test1 that holds a value derived from some other processing. What I need to do is use that $test1 as the input to a awk regular expression: nawk -F"," -v tester=$test1 ' /tester/{ print $0 } ' $inputFile So what I have is tester... (6 Replies)
Discussion started by: not4google
6 Replies

9. Shell Programming and Scripting

Variable Initialize

Hi, i m trying to loop a variable value, Basically i m reading variable value from an input file and using in rest of the program. Bu my problem is when i first time read value and assign to variable it works fine, but in second read it concatinate second value to first value with a LF. I want to... (2 Replies)
Discussion started by: coolbudy
2 Replies

10. Programming

variable initialize question

I have a question about C program for Unix: Do we have to intialize every variable before we use them in C? I thought we have to untill I found some programs are not intialize the variable before they were used. (2 Replies)
Discussion started by: whatisthis
2 Replies
Login or Register to Ask a Question