Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 7th, 2004, 10:49 AM   #1
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
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
keenlearner is offline   Reply With Quote
Old Sep 7th, 2004, 11:34 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 7th, 2004, 1:11 PM   #3
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
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...
keenlearner is offline   Reply With Quote
Old Sep 7th, 2004, 6:45 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 7th, 2004, 9:20 PM   #5
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
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.
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Sep 8th, 2004, 1:33 PM   #6
keenlearner
Newbie
 
Join Date: Sep 2004
Posts: 13
Rep Power: 0 keenlearner is on a distinguished road
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
keenlearner is offline   Reply With Quote
Old Sep 8th, 2004, 8:46 PM   #7
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
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'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Sep 9th, 2004, 8:51 PM   #8
sarumont
Hobbyist Programmer
 
sarumont's Avatar
 
Join Date: Apr 2004
Location: /dev/urandom
Posts: 154
Rep Power: 5 sarumont is on a distinguished road
Send a message via ICQ to sarumont Send a message via AIM to sarumont Send a message via Yahoo to sarumont
Dupe, as called by erebus. B)
__________________
"Time is an illusion. Lunchtime doubly so."
-the late, great Douglas Adams
sarumont is offline   Reply With Quote
Old Sep 9th, 2004, 10:42 PM   #9
erebus
Programmer
 
erebus's Avatar
 
Join Date: Aug 2004
Location: /dev/null
Posts: 65
Rep Power: 5 erebus is on a distinguished road
Send a message via AIM to erebus
I wish I got the joke, dude :-P.
__________________
<span style='font-size:14pt;line-height:100%'><span style='color:red'>&quot;Political power grows out of the barrel of a gun&quot; - Mao Tse-Tung</span></span>
erebus is offline   Reply With Quote
Old Feb 27th, 2005, 4:34 PM   #10
Vorlin
Newbie
 
Join Date: Feb 2005
Posts: 12
Rep Power: 0 Vorlin is on a distinguished road
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...

Last edited by Vorlin; Feb 27th, 2005 at 4:35 PM. Reason: Forgot to add tail part...
Vorlin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:13 PM.

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