![]() |
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 :) |
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. |
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: |
Oh... sorry, I mis-read. The easiest way would be to set an environment variable and read in that variable into the other program.
|
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 }':
awk '{ print ENVIRON["USER"] }'For your problem, it's a little bit tricky. Maybe something like: :
var=`awk '{ printf "%d", $1 }' filename` |
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 |
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. |
Dupe, as called by erebus. B)
|
I wish I got the joke, dude :-P.
|
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 }'`Or you could do something like this, just as an alternative: :
$ /home/booyaka > export FOO=`cat $HOME/foo.dat | head -1 | cut -d: -f1`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