Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variable out of AWK to shell 2

Status
Not open for further replies.

gawker

Programmer
Feb 21, 2002
34
US
Hi,

I have an AWK process in a shell script. Part of the function is to determine the position of a character string on a line. For example:

awk ' {
BytePos = match($0,"a")
print BytePos
} ' string.dat

I want to access the value of the BytePos variable by the shell script code that follows the AWK process.

How can this be done?

Thank you.

gawker
 
Hi gawker-

Try this:

Shell_Var = awk '{
BytePos = match($0,"a")
print BytePos
}' string.dat

echo Shell_Var # should return position

HTH


flogrr
flogr@yahoo.com

 
scriptByte=$(awk ' {
BytePos = match($0,"a")
print BytePos
} ' string.dat)
 
Hello again :)

Thank you very much for your responses, but I am not able to get the code to work. I tried both solutions and received different errors.
-----------------------------------
flogrr - This is the script and output from the solution you offered:
-----------------------------------

#! /bin/bsh
Shell_Var = awk ' {
BytePos = match($0,"a")
print BytePos
} ' string.dat
echo $Shell_Var

test33.ksh: Shell_Var: not found.

$
-----------------------------------
vgersh99 - This is the script and output from the solution you offered:
-----------------------------------

#! /bin/bsh
ptByte=$(awk ' {
BytePos = match($0,"a")
print BytePos
} ' string.dat)
echo $ptByte

test33.ksh: Syntax error at line 4: `ptByte=$' not expected.
$
------------------
Any ideas on what I might be doing wrong?

Thanks you.

gawker
 
A quick note.

I'm attempting to run the script on an AIX 4.3.3.0 system(where I need it).

Just for kicks, I ran the solutions on a LINUX 7.2 system. vgersh99's solution works, but flogrr's gives back "ShellVar: command not found".
 
I don't know what "#! /bin/bsh" is on AIX

Try it with "#! /bin/ksh"

vlad
 
It was a good idea, but "#! /bin/ksh" gave me the same results (bsh is the Borne Shell)
 
It was a good idea, but "#! /bin/ksh" gave me the same results (bsh is the Borne Shell)
 
The solution I posted should have had the awk script (including the input file) enclosed in back ticks ( ` ) .

Also, bsh is the Bourne Again Shell.

I recommend, using sh which is the Bourne Shell.



flogrr
flogr@yahoo.com

 
Hi gawker,

In the flogrr's script, remove spaces before and after the equal sign and use the $(awk ... ) syntax like in the vgersh99's script (or `awk ...` syntax). Jean Pierre.
 
TRY
ptByte=`awk ' {
        BytePos = match($0,"a")
        print BytePos
       } ' string.dat`
 
I tried flogrr's response and it worked just fine. I'll try working with the other one later.

Thank you all for you time and responses - you've been a big help.

gawker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top