![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 13
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2004
Posts: 13
Rep Power: 0
![]() |
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... ![]() |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
Oh... sorry, I mis-read. The easiest way would be to set an environment variable and read in that variable into the other program.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Programmer
|
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`
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>"Political power grows out of the barrel of a gun" - Mao Tse-Tung</span></span> |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2004
Posts: 13
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
|
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.
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>"Political power grows out of the barrel of a gun" - Mao Tse-Tung</span></span> |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Dupe, as called by erebus. B)
__________________
"Time is an illusion. Lunchtime doubly so." -the late, great Douglas Adams |
|
|
|
|
|
#9 |
|
Programmer
|
I wish I got the joke, dude :-P.
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>"Political power grows out of the barrel of a gun" - Mao Tse-Tung</span></span> |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2005
Posts: 12
Rep Power: 0
![]() |
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
10Or 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... Last edited by Vorlin; Feb 27th, 2005 at 4:35 PM. Reason: Forgot to add tail part... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|