Setting variables from awk (c shell)

Setting variables from awk (c shell)

Post by Jase » Sat, 29 May 1999 04:00:00



I am new to scripting and awk but have an 'easy' problem that I can't
solve.

How can I get a value from awk into a variable that I am using in a C
shell script?
e.g. I use awk thus:
awk 'NR==1 {print $1}'<inputfile     #prints 1st field of 1st line of
file 'inputfile'

I want to store the output of this into a variable which I use later in
my C script.  I have tried piping it but can't get it right.  I have
also tried set variable = "awk...." but this doesn't work.
Although I can set the variable within the awk script, I can't get it
out into my C script.  Ideas?

In addition to this, after having performed a number of operations, I
want to repeat the entire program, but this time setting NR=2 and so
on.  I can't seem to set NR from outside of the awk script. What am I
doing wrong?

Lastly, the file I have doesn't have constant fields for each line,
mainly because there is some text in each line with spaces.  As such,
awk gives a different field number for each line.  Since I am trying to
make the script compatible for both linux and solaris systems, I can't
use the FIELDWIDTHS setting.  Is there any way that I can set constant
field widths so that the parts containing text and spaces is treated as
one field?

Hope someone can help
TIA.
Jase


 
 
 

Setting variables from awk (c shell)

Post by Ralf Draege » Sat, 29 May 1999 04:00:00



> I am new to scripting and awk but have an 'easy' problem that I can't
> solve.

> How can I get a value from awk into a variable that I am using in a C
> shell script?
> e.g. I use awk thus:
> awk 'NR==1 {print $1}'<inputfile     #prints 1st field of 1st line of
> file 'inputfile'

> I want to store the output of this into a variable which I use later in
> my C script.  I have tried piping it but can't get it right.  I have
> also tried set variable = "awk...." but this doesn't work.

set variable=`awk ...`
Quote:> Although I can set the variable within the awk script, I can't get it
> out into my C script.  Ideas?

Since I don't use csh I had to look in the manpages, it took me about 20
sec to figure this one out. man is your friend. Always do "man command" and
"more comp.unix.shell.faq" before posting questions.

HTH, Ralf.
--

- Intraplan Consult Gmbh  Orleansplatz 5a  81667 Muenchen  +49 89 45911-0 -

Mens sana in campari soda.               * <--- Tribble   . <--- Tribble.gz

 
 
 

Setting variables from awk (c shell)

Post by Nick Wag » Sat, 29 May 1999 04:00:00



> I am new to scripting and awk but have an 'easy' problem that I can't
> solve.

> How can I get a value from awk into a variable that I am using in a C
> shell script?
> e.g. I use awk thus:
> awk 'NR==1 {print $1}'<inputfile     #prints 1st field of 1st line of
> file 'inputfile'

Which shell are you using?  In the C-shell try:

set var = `awk 'NR==1 {print $1}' inputfile`
echo $var

Note the back quotes for substitution and the fact that if awk has no
standard input it will read from the file on its command line.

Quote:> I want to store the output of this into a variable which I use later in
> my C script.  I have tried piping it but can't get it right.  I have
> also tried set variable = "awk...." but this doesn't work.
> Although I can set the variable within the awk script, I can't get it
> out into my C script.  Ideas?

> In addition to this, after having performed a number of operations, I
> want to repeat the entire program, but this time setting NR=2 and so
> on.  I can't seem to set NR from outside of the awk script. What am I
> doing wrong?

What you are doing wrong is trying to set a variable that awk always
sets for itself.

Quote:> Lastly, the file I have doesn't have constant fields for each line,
> mainly because there is some text in each line with spaces.  As such,
> awk gives a different field number for each line.  Since I am trying to
> make the script compatible for both linux and solaris systems, I can't
> use the FIELDWIDTHS setting.  Is there any way that I can set constant
> field widths so that the parts containing text and spaces is treated as
> one field?

Probably. Try experimenting with regular expressions.
--

Software Project Manager                  Science Park, Milton Rd,
Tel: +44(0)1223 420414 (ext 213)          Cambridge, CB4 0FY, UK.
Fax: +44(0)1223 420044                    http://www.laser-scan.com/
Opinions expressed are attributable to me, not my employer.
 
 
 

Setting variables from awk (c shell)

Post by Harlan Grov » Sat, 29 May 1999 04:00:00




<snip>
Others have answered the question how to store awk output in a shell
variable. I'll attack the underlying problem.

Quote:> In addition to this, after having performed a number of operations, I
> want to repeat the entire program, but this time setting NR=2 and so
> on.  I can't seem to set NR from outside of the awk script. What am I
> doing wrong?

Looks like you want to run some shell commands with a _series_ of shell
variables each set using output from an awk script. Presumably you're
trying to have the awk script read through it's input only once.

One possibility: instead of wrapping the awk script inside the shell
script, call the shell script from the awk script. That is, instead of
printing from awk each time you've derived the value of the shell
variable, use awk's system() command to call the script with the value.
Here's an example with input consisting of two fields: $1 is an action -
process or skip; $2 is a filename.

awk '$1 == "process" { system("process '" $2 "'") }' inputfile

Moving on to pedagogy, NR is an awk system variable. You can read it,
but you shouldn't try to write to it. Even if you did, awk would defeat
you the next time it read anything. I think you want to advange to a
given record before processing that (and subsequent?) records. To do
so, try

awk -v skipto=5 'NR < skipto { next }; . . .'

Quote:> Lastly, the file I have doesn't have constant fields for each line,
> mainly because there is some text in each line with spaces.  As such,
> awk gives a different field number for each line.  Since I am trying
> to make the script compatible for both linux and solaris systems, I
> can't use the FIELDWIDTHS setting.  Is there any way that I can set
> constant field widths so that the parts containing text and spaces
> is treated as one field?

Looks like you're using gawk. If you have gawk on both systems, you
have FIELDWIDTHS on both systems, so you can use it. If you mean that
you need a single script that works under both gawk and other awks, you
get to parse your fixed width fields with substr.

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 
 
 

Setting variables from awk (c shell)

Post by bmar.. » Sun, 30 May 1999 04:00:00



   >comp.unix.questions:131427 I am new to scripting and awk but have
   >an 'easy' problem that I can't solve.
   >How can I get a value from awk into a variable that I am using in a
   >C shell script?
   >e.g. I use awk thus:
   >awk 'NR==1 {print $1}'<inputfile     #prints 1st field of 1st line
   >of file 'inputfile'
   >I want to store the output of this into a variable which I use
   >later in my C script.  I have tried piping it but can't get it
   >right.  I have also tried set variable = "awk...." but this doesn't
   >work.
You're using the wrong quote.  set variable = `awk...`
   >Although I can set the variable within the awk script, I
   >can't get it out into my C script.  Ideas?
   >In addition to this, after having performed a number of operations,
   >I want to repeat the entire program, but this time setting NR=2 and
   >so on.  I can't seem to set NR from outside of the awk script. What
   >am I doing wrong?
Again, it's a matter of quoting.  And you don't really want to set NR, you
set another variable and compare it with NR.  There are a few ways to use
a shell variable in an awk script, for example:
awk "NR==$whichline "'{print $1}'
awk 'NR==which {print $1}' which=$whichline
   >Lastly, the file I have doesn't have constant fields for each line,
   >mainly because there is some text in each line with spaces.  As
   >such, awk gives a different field number for each line.  Since I am
   >trying to make the script compatible for both linux and solaris
   >systems, I can't use the FIELDWIDTHS setting.  Is there any way
   >that I can set constant field widths so that the parts containing
   >text and spaces is treated as one field?
I think you could use substr().  If you've ever used Basic, substr in awk
is like MID$ in Basic.

Net-Tamer V 1.08X - Test Drive