Return Awk Variable to Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return Awk Variable to Shell
# 8  
Old 08-07-2009
Two solutions:
1. using eval
2. using IFS, which fpmurphy also demonstrated

Code:
#! /bin/sh

LINE="CLIENT:0,3,6,9,12,15,18,21:/home/erm/user/CLIENT/PARM/unix_servers.txt:/home/erm/user/CLIENT/PARM/nt_servers.txt:/home/erm/user/CLIENT/PARM/CLIENT_url.txt:/home/erm/user/COACH/PARM/ping_serv_list.txt:/home/erm/user/COACH/PARM/pass.txt"

#
# solution 1 - two passes using eval
#
eval `echo $LINE | awk -F: '{printf("account=\"%s\";schedule=\"%s\";unixlist=\"%s\";ntlist=\"%s\";urllist=\"%s\";pinglist=\"%s\";ntpassfile=\"%s\";",$1,$2,$3,$4,$5,$6,$7)}'`
echo "SOLUTION-1:"
echo "account=$account"
echo "schedule=$schedule"
echo "unixlist=$unixlist"
echo "ntlist=$ntlist"
echo "urllist=$urllist"
echo "pinglist=$pinglist"
echo "ntpassfile=$ntpassfile"

#
# solution 2 - set IFS to be colon to help filtering
#
IFS_original=$IFS
IFS=":"
set -- $LINE
echo "SOLUTION-2:"
echo "account=$1"
echo "schedule=$2"
echo "unixlist=$3"
echo "ntlist=$4"
echo "urllist=$5"
echo "pinglist=$6"
echo "ntpassfile=$7"
IFS=$IFS_original

# 9  
Old 08-07-2009
Code:
oifs="$IFS"
line="a:b:c"

# only ksh
echo $line | IFS=":" read v1 v2 v3 vx
echo "A v1:$v1, v2:$v2"

# ksh+bash:
IFS=":" read v1 v2 v3 vx <<EOF
$line
EOF
echo "C v1:$v1, v2:$v2"

# ksh+bash:
IFS=":"
array=( $line )
IFS="$oifs"
echo "D v1:${array[0]}, v2:${array[1]}"

And all previous ...

In ksh you can use also set to array (compare chihung set), save current IFS, set IFS=:" and return IFS. Values are in the array, starting index 0.
Code:
set -A array -- $line

Using awk to parse only one line, awk is slowest, but if whole script has done using awk, awk is much faster as ksh/bash/sh script.
# 10  
Old 08-07-2009
thank you everyone.. got it, happy scripting.
# 11  
Old 08-08-2009
Using bash shell arrays

IFS=: comps=($line)

will do it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Shell Variables passed to awk to return certain rows

Hi Forum. I have the following test.txt file and need to extract certain rows based on "starting position", "length of string" and "string to search for": 1a2b3d 2a3c4d ..... My script accepts 3 parameters: (starting col pos, length to search for, string to search for) and would like to... (4 Replies)
Discussion started by: pchang
4 Replies

3. Shell Programming and Scripting

Return value inside isql to a shell variable in ksh

Hello, I have a shell script where I am doing an isql to select some records. the result i get from the select statement is directed to an output file. I want to assign the result to a Shell variable so that I can use the retrieved in another routine. e.g. "isql -U${USER} -P${PASSWD} -S${SERVER}... (1 Reply)
Discussion started by: RookieDev
1 Replies

4. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

5. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

6. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

7. Shell Programming and Scripting

Awk return variable

Hi I have 2 working script, now i'd like to get the return value from the first and give it to the 2 script (both script work correctly if I run it separately). so i think the problem is only the first line in the way i pass the variable. in the final the "print lst", is just to check the... (2 Replies)
Discussion started by: Dedalus
2 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

10. Shell Programming and Scripting

return variable from PL/SQL procedure to shell

Hi i'm calling a pl/sql procedure which is returning one variable. i'm trying to assing this value to variable in shell script the code i wrote is ** in shell script** var= 'sqlplus user/pass @ret.sql' echo $var ** and variable dum_var number exec rt_test(:DUM_VAR); exit; in... (4 Replies)
Discussion started by: ap_gore79
4 Replies
Login or Register to Ask a Question