Operating System - Linux
1751937 Members
4579 Online
108783 Solutions
New Discussion юеВ

getline and format in awk

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

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
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

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...
James R. Ferguson
Acclaimed Contributor
Solution

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...

lawrenzo
Trusted Contributor

Re: getline and format in awk

magic,

it works a treat James ...

Thanks again.

Chris. (MORECOFEE)
hello
Dennis Handly
Acclaimed Contributor

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.