Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   Awk Help Needed...urgent! (http://www.programmingforums.org/showthread.php?t=484)

keenlearner Sep 7th, 2004 10:49 AM

hi!

how can i store the output of awk command in a script..
for eg:

in TRY.sh

awk '{ printf "%d", $1 }' filename
var=$1

i want something of this sort...
but this particular thing does'nt work..
i mean if the $1 value is 50 than i am not able to store it in a different variable
for further calculations..

so pls help me out...

TAKE CARE
Regards
Keen Learner :)

Infinite Recursion Sep 7th, 2004 11:34 AM

your_awk_command > yourfile

awk -F":" '/Grendel/{print $1 " - " $2 " - " $3}' data.txt > results.txt

The results of the parsing of data.txt will be stored in results.txt.

let us know if you have problems with this.

keenlearner Sep 7th, 2004 1:11 PM

It works definetely.. but..
i dont want to store the output in another file..
i want the output in a variable so that i can further use that value in my script.

actually i am picking a particular value from a file..
and that is the first field of dat file..
i am able to get it but cannot able to store it..

i want it in a variable...dat value is a numeric value.

hope u understand my problem... :rolleyes:

Infinite Recursion Sep 7th, 2004 6:45 PM

Oh... sorry, I mis-read. The easiest way would be to set an environment variable and read in that variable into the other program.

erebus Sep 7th, 2004 9:20 PM

Actually, most newer implementations of awk (POSIX, Nawk, or Gawk) has the -v switch so you can interface awk and shell variables. Something like this should suffice:
:

awk -v name="$USER" '{ print name }'
Or you can use a built-in associative array that accepts a current environment variable name as its index, which is quite handy for working with the shell:
:

awk '{ print ENVIRON["USER"] }'
Another less efficient way would be to use the export command, though I won't give an example because I like the above methods, but it can be done.

For your problem, it's a little bit tricky. Maybe something like:
:

var=`awk '{ printf "%d", $1 }' filename`
Not sure if that's what you were looking for, but working with the above information will get you where you need to go. This will substitue anything in back-quotes with the output of the command sequence within the two, thus, saving what should have gone to stdout to a variable. Sorry I couldn't be of much help.

keenlearner Sep 8th, 2004 1:33 PM

definitely i want something like..

var=`awk '{printf "%d".$1}' filename`

but this particular command will not work...
this is the problem i am facing that i am not able to write that particular syntax of awk....

could u pls suggest something more...

Thanx
With Regards

erebus Sep 8th, 2004 8:46 PM

What shell are you using that doesn't let you use this to put command output in a variable. If you're using csh or tcsh, use set to do variables(man set). If not, wish ya luck...

EDIT: I noticed that the extension on your script is *.sh, so you probably want to use that. If sh is not your default shell(which i'm not making the assumption it's not, but it's a possibilty), use the shebang line at the beggining of the script(#!/bin/sh). If you don't know what your shell is, 'echo $SHELL' and get back to us.

sarumont Sep 9th, 2004 8:51 PM

Dupe, as called by erebus. B)

erebus Sep 9th, 2004 10:42 PM

I wish I got the joke, dude :-P.

Vorlin Feb 27th, 2005 4:34 PM

In trying to understand your script and what you're doing with it, here's what I did.

I created a 'foo.dat' file in my home directory ($HOME in env var). In this file, there's a simple line that has '10:20:30:40' in it. I didn't know the format of your dat file but you said it was the first variable so here we go.

Then I created a global variable that was exported. Something to remember about exporting a variable from the command line is that it will only be available in that session unless you add it as an alias executed during your login or add it to the skeleton profile, etc...

:

$ /home/booyaka > export FOO=`cat $HOME/foo.dat | head -1 | awk -F: '{ print $1 }'`
$ /home/booyaka > echo $FOO
10


Or you could do something like this, just as an alternative:
:

$ /home/booyaka > export FOO=`cat $HOME/foo.dat | head -1 | cut -d: -f1`
$ /home/booyaka > echo $FOO
10


If you're trying to do something else, and I misinterpreted, my bad...


All times are GMT -5. The time now is 12:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC