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!

help with awk "if"

Status
Not open for further replies.

beowulfkid

Programmer
Apr 4, 2008
2
CA
My input is as below:
Locks:
Address TranHdl Lockname Type Mode Sts Owner Dur HldCnt Att ReleaseFlg
0x07800000208E0340 3 53514C4332453037DF495EFE41 Internal P ..S G 3 1 0 0x0000 0x40000000
0x07800000208DB000 7 53514C4332453037DF495EFE41 Internal P ..S G 7 1 0 0x0000 0x40000000
0x07800000208DA380 7 00000038000000010001A40056 Internal V ..S G 7 1 0 0x0000 0x40000000
0x07800000208DB280 3 0000000200007F0422FB468043 CatCache ..S G 3 1 0 0x0000 0x40000000
0x07800000208E49C0 3 00040101000000000000000054 Table ..X G 3 255 0 0x0000 0x40000000
0x07800000208DC1C0 7 00040101000000000000000054 Table .IS W* 7 1 0 0x0000 0x00000001

example of what I want to do is below:

$ more locks1556528 | awk '$3 == "00040101000000000000000054" {print $0}'
0x07800000208E49C0 3 00040101000000000000000054 Table ..X G 3 255 0 0x0000 0x40000000
0x07800000208DC1C0 7 00040101000000000000000054 Table .IS W* 7 1 0 0x0000 0x00000001

The problem I am trying to solve here is "I do not know the value 00040101000000000000000054" dynamically to compare the $3 with. How I get this value is It is $3 for the row in which $6 = W* as shown below:

0x07800000208DC1C0 7 00040101000000000000000054 Table .IS W* 7 1 0 0x0000 0x00000001

I want to print those two rows in which the $3 is same.
But, this value is the $3 for the row in which $6==W*

Please help !
Thanks Gurus.
 
So you meen: Search through the file for a line with W* in $6,
then print all lines with the same value in $3 as that line?

Will there only be one line with W* in $6 every time?

What have you tried yourself?
 
Yes. There would be only W*.
Yes. Search through the file that has W* in $6, then print all lines with the same value in $3 as that line.

My script:
var1=`cat locks2 | awk '$6 == "W*" {print $3}'`
echo $var1
cat locks2 | awk '$3 == var1 {print $0}'

Output:
00040101000000000000000054

My desired output:
0x07800000208E49C0 3 00040101000000000000000054 Table ..X G 3 255 0 0x0000 0x40000000
0x07800000208DC1C0 7 00040101000000000000000054 Table .IS W* 7 1 0 0x0000 0x00000001
 
You don't need to use cat or more like that, awk will read its own input files.

You can't reference a shell variable within an awk script. awk variables are a completely separate thing, but you can assign a value to the variable on the awk[b/] command line first:

Code:
var1=`awk '$6 == "W*" {print $3}' locks2`
echo $var1
awk -v awkvar1=$var1 '$3 == awkvar1' locks2

Or you can use shell quoting tricks to insert it into your awk code:

Code:
var1=`awk '$6 == "W*" {print $3}' locks2`
echo $var1
awk '$3 == "'$var1'"' locks2

But I think you'll agree that's messy and a little confusing.

Note also that you can leave out the { print $0 } since that is the default action when the condition is true.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top