![]() |
|
![]() |
|
|
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 | |
|
Newbie
Join Date: May 2005
Posts: 1
Rep Power: 0
![]() |
same problem
I'm having the same problem and was wondering if you found a fix.
I want to store the output of an awk command into a variable to be used later. Have file names first last cuid awk '{print $2 ; }' names will list last names last='awk '{print $2 ; }' names' will not work any suggestions? I think I have to do something with the quotes to get the output of the awk to go into the variable last. Quote:
|
|
|
|
|
|
|
#8 |
|
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> |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
Dupe, as called by erebus. B)
__________________
"Time is an illusion. Lunchtime doubly so." -the late, great Douglas Adams |
|
|
|
|
|
#10 |
|
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> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|