Assigning a field variable in awk program to a shell variable - awk
This is a discussion on Assigning a field variable in awk program to a shell variable - awk ; hi
myself a newbie to unix shell scripting and stuff..
i want to assign a field variable in an awk program to a shell
variable.
like for eg
if shell_var is one of a shell variable
then i want to ...
-
Assigning a field variable in awk program to a shell variable
hi
myself a newbie to unix shell scripting and stuff..
i want to assign a field variable in an awk program to a shell
variable.
like for eg
if shell_var is one of a shell variable
then i want to assign the line numbers returned by the grep command
with -n option
to a shell variable.
the code that i've tried is this
grep -n string filename | awk -F : '{shell_var = $1}'
it doesn't work
please help..
-
Re: Assigning a field variable in awk program to a shell variable
deeplights@ wrote:
> hi
> myself a newbie to unix shell scripting and stuff..
> i want to assign a field variable in an awk program to a shell
> variable.
> like for eg
> if shell_var is one of a shell variable
> then i want to assign the line numbers returned by the grep command
> with -n option
> to a shell variable.
>
> the code that i've tried is this
> grep -n string filename | awk -F : '{shell_var = $1}'
First, you don't need grep if you use awk. The above can be written as
awk '/string/ {shell_var = NR}' filename
but here 'shell_var' is an awk variable.
>
> it doesn't work
Now to make the above work
shell_var=$( awk '/string/{print NR; exit}' filename )
I added 'exit' because you just want a single line number in the shell
variable.
Janis
>
> please help..
>
-
Re: Assigning a field variable in awk program to a shell variable
Janis Papanagnou wrote:
> deeplights@ wrote:
>
>> hi
>> myself a newbie to unix shell scripting and stuff..
>> i want to assign a field variable in an awk program to a shell
>> variable.
>> like for eg
>> if shell_var is one of a shell variable
>> then i want to assign the line numbers returned by the grep command
>> with -n option
>> to a shell variable.
>>
>> the code that i've tried is this
>> grep -n string filename | awk -F : '{shell_var = $1}'
>
>
> First, you don't need grep if you use awk. The above can be written as
>
> awk '/string/ {shell_var = NR}' filename
>
> but here 'shell_var' is an awk variable.
>
>>
>> it doesn't work
>
>
> Now to make the above work
>
> shell_var=$( awk '/string/{print NR; exit}' filename )
>
> I added 'exit' because you just want a single line number in the shell
> variable.
>
> Janis
And if you want to pass the value of a shell variable to an awk script
in future, see question 24 in the comp.unix.shell FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24).
Ed.
Similar Threads
-
By Application Development in forum CSharp
Replies: 23
Last Post: 12-17-2007, 05:02 AM
-
By Application Development in forum Sharepoint
Replies: 1
Last Post: 06-28-2007, 10:03 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 05-16-2007, 07:57 PM
-
By Application Development in forum Clipper
Replies: 1
Last Post: 12-26-2005, 05:32 PM
-
By Application Development in forum basic.visual
Replies: 3
Last Post: 12-09-2003, 11:37 AM