Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script question 1

Status
Not open for further replies.

lambros

Programmer
Oct 10, 2002
42
US
I am using awk, If I use this command...

if ( $0 ~ /Test/ )

How do I set a variable to the $1 of this output. I've tried

var=$1

but this sets var to every $1 in my input - which is no good to me, I need to set var to $1 of the string returned by /Test/... Can I write some if statement to do this, I've tried arrays etc. but I still can't get this value out.... Thanks
 
Hi jamisar,

Thanks for the qick reply, but the solution is not working for me... I need to return $1 only where the string matches /test/ & assign this to vaiable.... The solution you provided returns ever $1 of the input file... could this be a bracket problem?
 
Hi jamisar,

apoligies - you were of course correct, this does work for me... Thanks, now another question, should this work if I need to add the value from var to an array?

/test/{ping=$1; reg[1]=ping}
 
It does jamisar, however when I write something like;

reg[1]=$1; print reg[1]

I get the correct output, however if I write

reg[1]=$1; print reg[1]

for (i in reg)
print i


I don't get the correct information back, it just those entries I put into array reg[1].... I thought this for loop should work, however it is not... can you help?

 
i solved it using

for (i in reg)
print reg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top