getline and format in awk (244 Views)
Reply
Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 1 of 5 (244 Views)
Accepted Solution

getline and format in awk

Hello,

I want to search a list of installed files (AIX) and I find that the list is not consistent with $1 being the software and $2 being the version. Sometimes in the list $2 in on the next line:

ldap.client.adt 5.2.0.0 COMMITTED Directory Client SDK
ldap.client.rte 5.2.0.0 COMMITTED Directory Client Runtime (No
SSL)
ldap.max_crypto_client.adt
5.2.0.0 COMMITTED Directory Client SDK
ldap.max_crypto_client.rte
5.2.0.0 COMMITTED Directory Client Runtime (SSL)


so I thought I could solve this using getline which I can if I search the pattern and run the below command:

# lslpp -l |awk '/ldap.max/ {(x = $0);getline;print x,$1}'

ldap.max_crypto_client.adt 5.2.0.0
ldap.max_crypto_client.rte 5.2.0.0

but if I search multiple string and check that if $2 is equal to null I get a strange output:

# lslpp -l |egrep "ldap|gsk"
gskak.rte 6.0.5.41 COMMITTED AIX Certificate and SSL Base
gskta.rte 7.0.4.14 COMMITTED AIX Certificate and SSL Base
ldap.client.adt 5.2.0.0 COMMITTED Directory Client SDK
ldap.client.rte 5.2.0.0 COMMITTED Directory Client Runtime (No
ldap.max_crypto_client.adt
ldap.max_crypto_client.rte
ldap.client.rte 5.2.0.0 COMMITTED Directory Client Runtime (No

# lslpp -l |awk '/ldap|gsk/ {if ($2 == "")(x = $0);getline;print x,$1}'
Runtime
Runtime
ldap.client.rte
ldap.max_crypto_client.adt 5.2.0.0
ldap.max_crypto_client.rte 5.2.0.0
ldap.max_crypto_client.rte SSL)

any ideas?

Thanks

Chris
hello
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 5 (244 Views)

Re: getline and format in awk

Hi Chris:

Using your data:

# awk '/ldap|gsk/ {if (NF==1){next} else {print $1,$2}}' file
gskak.rte 6.0.5.41
gskta.rte 7.0.4.14
ldap.client.adt 5.2.0.0
ldap.client.rte 5.2.0.0
ldap.client.rte 5.2.0.0

Isn't that what you want?

Regards!

...JRF...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (244 Views)

Re: getline and format in awk

Hi (agaih) Chris:

Sorry, I used the "strange output" from your first approximation as my input.

Using this:

# cat .myinput
ldap.client.adt 5.2.0.0 COMMITTED Directory Client SDK
ldap.client.rte 5.2.0.0 COMMITTED Directory Client Runtime (No
SSL)
ldap.max_crypto_client.adt
5.2.0.0 COMMITTED Directory Client SDK
ldap.max_crypto_client.rte
5.2.0.0 COMMITTED Directory Client Runtime (SSL)

...and:

# awk '/ldap|gsk/ {if (NF==1){x=$0;getline;print x,$1} else {print $1,$2}}' ./myfile
ldap.client.adt 5.2.0.0
ldap.client.rte 5.2.0.0
ldap.max_crypto_client.adt 5.2.0.0
ldap.max_crypto_client.rte 5.2.0.0

Regards! [ENOCOFFEE]

...JRF...

Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 4 of 5 (244 Views)

Re: getline and format in awk

magic,

it works a treat James ...

Thanks again.

Chris. (MORECOFEE)
hello
Acclaimed Contributor
Dennis Handly
Posts: 25,199
Registered: ‎03-06-2006
Message 5 of 5 (244 Views)

Re: getline and format in awk

>{(x = $0);getline;print x,$1}

(I'm not sure why you used ()?)

getline will also let you input into a variable:
{ getline second; print $0, second }

But I suppose if you want $1, you need to do it your way.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.