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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If I use this command... if ( $0

Status
Not open for further replies.

lambros

Programmer
Oct 10, 2002
42
US
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
 
Okay, I'm having trouble following you, but here goes.

if ($0 ~ /Test/) {
array[a++] = $1
}
END {
for (i in array) {
print array
}
}
Will work.


If instead you want $1 exported as a shell variable in a shell list or array..


function myvar() {
pat=$1
fname=$2

ans=$(awk -v p=$pat' {
if ($0 ~ p) {
print $1
}
}' $fname)

mylist="$mylist $ans"
export mylist
}

Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top