Hi all
I am new to awk. Trying to parse "test" from the following input
from inside a shell script.
a=test.val.something
echo $a | awk '{ print substr($1,1,4) }'
The above works fine, but I would like the output (or return
value from awk) in another shell variable.
I tried doing the following.
Trial 1 (did not work):
================
a=test.val.something
b=var
echo $a | awk '{ $'$b' = substr($1,1,4) }'
echo $var,$b
Trial 2 (did not work):
================
a=test.val.something
b=var
echo $a | awk '{ $'$b' = substr($1,1,4) } END { export $'$b'}'
echo $var,$b
Thanks in advance
Sri
|
|
0
|
|
|
|
Reply
|
srihari.raghavan (5)
|
6/13/2006 5:34:47 PM |
|
In article
<1150220087.299486.229460@h76g2000cwa.googlegroups.com>,
srihari.raghavan@gmail.com wrote:
> Hi all
> I am new to awk. Trying to parse "test" from the following input
> from inside a shell script.
>
> a=test.val.something
> echo $a | awk '{ print substr($1,1,4) }'
>
> The above works fine, but I would like the output (or return
> value from awk) in another shell variable.
>
> I tried doing the following.
>
> Trial 1 (did not work):
> ================
> a=test.val.something
> b=var
> echo $a | awk '{ $'$b' = substr($1,1,4) }'
> echo $var,$b
>
> Trial 2 (did not work):
> ================
> a=test.val.something
> b=var
> echo $a | awk '{ $'$b' = substr($1,1,4) } END { export $'$b'}'
> echo $var,$b
>
> Thanks in advance
> Sri
b=`echo $a | awk '{ print substr($1,1,4) }'`
Notice the grave accent (reverse quote) before and after the
echo/awk statements. This is how you substitute the output from
executed commands into a shell command line.
Or if you have to return multiple values use
set -- `awk '/pattern/ { print $5 }' some.input.file`
for j
do
echo "$j"
done
This would find all the lines with 'pattern' in them, and print
out the 5th field which would be set up as shell variables $1, $2,
$3, $4, $5, .....
The for loop, loops through all the shell arguments and echos them
out.
If you know exactly how many values you are returning, then you
just access them via $1, $2, $3, etc....
Bob Harris
|
|
0
|
|
|
|
Reply
|
Bob
|
6/14/2006 12:16:10 AM
|
|
On 2006-06-13, srihari.raghavan@gmail.com wrote:
> Hi all
> I am new to awk. Trying to parse "test" from the following input
> from inside a shell script.
>
> a=test.val.something
> echo $a | awk '{ print substr($1,1,4) }'
>
> The above works fine, but I would like the output (or return
> value from awk) in another shell variable.
>
> I tried doing the following.
>
> Trial 1 (did not work):
> ================
> a=test.val.something
> b=var
> echo $a | awk '{ $'$b' = substr($1,1,4) }'
> echo $var,$b
>
> Trial 2 (did not work):
> ================
> a=test.val.something
> b=var
> echo $a | awk '{ $'$b' = substr($1,1,4) } END { export $'$b'}'
> echo $var,$b
I wouldn't use awk; the shell can do it:
var=${a%%.*}
Or, in a Bourne shell"
IFS=.
set -f
set $a
var=$1
With awk, you would use command substitution:
var=$( echo "$a" | awk -F. '{print $1}' )
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
0
|
|
|
|
Reply
|
Chris
|
6/14/2006 12:16:16 AM
|
|
srihari.raghavan@gmail.com wrote:
> Hi all
> I am new to awk. Trying to parse "test" from the following input
> from inside a shell script.
>
> a=test.val.something
> echo $a | awk '{ print substr($1,1,4) }'
Have you tried this:
$ b=`echo $a | awk '{ print substr($1,1,4) }'`
You should find a good bash reference and find out what the back-quote
"`" does. The 'Bash-programming-intro' Linux Howto I do not recommend.
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
'The Unix Programming Environment' by Kerninghan and Pike is a good
book. It is old though.
|
|
0
|
|
|
|
Reply
|
Prophet
|
6/14/2006 6:28:17 PM
|
|
srihari.raghavan@gmail.com wrote:
> Hi all
> I am new to awk. Trying to parse "test" from the following input
> from inside a shell script.
>
> a=test.val.something
> echo $a | awk '{ print substr($1,1,4) }'
Have you tried this:
$ b=`echo $a | awk '{ print substr($1,1,4) }'`
You should find a good bash reference and find out what the back-quote
"`" does. The 'Bash-programming-intro' Linux Howto I do not recommend.
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
'The Unix Programming Environment' by Kernighan and Pike is a good book.
It is old though.
|
|
0
|
|
|
|
Reply
|
Prophet
|
6/14/2006 6:28:43 PM
|
|
Hi all
Thank you all for your time. Works fine now.
Sri
|
|
0
|
|
|
|
Reply
|
srihari
|
6/15/2006 4:07:17 PM
|
|
|
5 Replies
297 Views
(page loaded in 3.331 seconds)
|