Assigning a value to variable through awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning a value to variable through awk
# 1  
Old 02-29-2012
Assigning a value to variable through awk

I am trying to assign a value to a variable thru awk and I am having a lot of problem with it. Pls see the code snippet. The one in RED is the actual code. Other lines are the op created by the system. As you can see from the data, I was expecting an output of DG010 SDS FILE for FILE_NAME variable. Any inputs is appreciated.

Quote:
grep -i $1 ${sFileRedirectList} > ${PARMDIR}/MailList_$$
+ grep -i DG010* /etl/estdm2/DEV/paramlib/FileRedirectList
+ 1> /etl/estdm2/DEV/paramlib/MailList_26143

cat ${PARMDIR}/MailList_$$
+ cat /etl/estdm2/DEV/paramlib/MailList_26143
DG010,DG010 SDS FILE,OUT,OUTGOING_DIR,/datamart/dm_sds_sftp/Outgoing/SDS_DEF,OUTGOING_DIR
DG010,DG010 SDS FILE,IN,/datamart/dm_sds_sftp/Incoming,INCOMING_DIR,INCOMING_DIR

FILE_NAME=$(awk -F, '$3~/"$sDirn"/ {print $2}' ${PARMDIR}/MailList_$$)
+ + awk -F, $3~/"$sDirn"/ {print $2} /etl/estdm2/DEV/paramlib/MailList_26143
FILE_NAME=

echo " file is " ${FILE_NAME}
+ echo file is
file is
# 2  
Old 02-29-2012
Code:
FILE_NAME=$(awk -F, -v d="$sDirn" '$3~d{print $2}' ${PARMDIR}/MailList_$$)

?
# 3  
Old 02-29-2012
That did not work.
# 4  
Old 02-29-2012
did you set the variable $sDirn ? to which value ?

what is the content of your file ${PARMDIR}/MailList_$$ ?
# 5  
Old 02-29-2012
Content of file is:
Code:
DG010,DG010 SDS FILE,OUT,OUTGOING_DIR,/datamart/dm_sds_sftp/Outgoing/SDS_DEF,OUTGOING_DIR
DG010,DG010 SDS FILE,IN,/datamart/dm_sds_sftp/Incoming,INCOMING_DIR,INCOMING_DIR

$sDirn is set to OUT

Last edited by Franklin52; 03-01-2012 at 03:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 02-29-2012
ctsgnb code works fine for me .. how did u try to run that ..??
Code:
$ sDirn=OUT
$ FILE_NAME=$(awk -F, -v d="$sDirn" '$3~d{print $2}' infile)
$ echo $FILE_NAME
DG010 SDS FILE
$

# 7  
Old 02-29-2012
ok, try with backquote instead of $( ... ) :

Code:
FILE_NAME=`awk -F, -v d="$sDirn" '$3~d{print $2}' ${PARMDIR}/MailList_$$`

---------- Post updated at 07:45 PM ---------- Previous update was at 07:30 PM ----------

Maybe you have an issue with the $$ notation ?

you could then give a try to:

Code:
MYFILE=${PARMDIR}/MailList_$$
FILE_NAME=$(awk -F, -v d="$sDirn" '$3~d{print $2}' $MYFILE )

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it. Background context: Post processing LAMMPS simulation data. tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles,... (10 Replies)
Discussion started by: ThomasP
10 Replies

2. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

3. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

4. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

5. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

Assigning value to a variable

Is there any difference between: set variable=39 and variable=39 (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

9. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

10. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies
Login or Register to Ask a Question