Operating System - HP-UX
1834351 Members
1965 Online
110066 Solutions
New Discussion

awk command question: equivalent to grep.?

 
SOLVED
Go to solution
rveri
Super Advisor

awk command question: equivalent to grep.?

All,

What is the awk command that is equivalent to "grep -i -e "

Example:
# swlist -l product | grep -i -e "serviceguard" -e "PHSS_34504"




Thanks,
11 REPLIES 11
Sandman!
Honored Contributor

Re: awk command question: equivalent to grep.?

try the one below...

# swlist -l product | awk '$1=="ServiceGuard" || $1=="PHSS_34504"'
Dennis Handly
Acclaimed Contributor

Re: awk command question: equivalent to grep.?

Why would you want to use the wrong tool?

The only thing I see in awk is that you can use tolower and then do the compare or regular expression match. To search for multiple patterns (-e), you would have to use ||.
Ivan Ferreira
Honored Contributor

Re: awk command question: equivalent to grep.?

swlist -l product | awk -v IGNORECASE=1 '/serviceguard|PHSS_34504/ { print $0}'
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
rveri
Super Advisor

Re: awk command question: equivalent to grep.?

Thanks,

Sandman: command working, you already using the correct string. :)

Ivan,
Looks like in your command IGNORECASE not working: Any way I got the idea.


# swlist -l product | awk -v IGNORECASE=1 '/serviceguard|PHSS_34504/ { print $0}'
PHSS_34504 1.0 Serviceguard A.11.16.00


# swlist -l product | awk -v IGNORECASE=1 '/ServiceGuard|PHSS_34504/ { print $0}'
PHSS_34504 1.0 Serviceguard A.11.16.00
ServiceGuard A.11.16.00 ServiceGuard


Thanks,
Dennis Handly
Acclaimed Contributor
Solution

Re: awk command question: equivalent to grep.?

>Ivan: awk -v IGNORECASE=1 '/serviceguard|PHSS_34504/ { print $0}'

HP's awk doesn't support IGNORECASE. But gawk seems to.
rveri
Super Advisor

Re: awk command question: equivalent to grep.?

Thanks Dennis, Works fine with gawk ( GNU Awk 3.0 ) :

hpap091# swlist -l product | gawk -v IGNORECASE=1 '/serviceguard|PHSS_34504/ { print $0}'
PHSS_34504 1.0 Serviceguard A.11.16.00
SG-Apache-Tool B.02.21 Serviceguard Apache Script Templates
SG-NFS-Tool A.11.23.03 Serviceguard NFS Script Templates
SG-Oracle-Tool B.02.21 Serviceguard Oracle Script Templates
SG-Samba-Tool B.02.21 Serviceguard Samba Script Templates
SG-Tomcat-Tool B.02.21 Serviceguard Tomcat Script Templates
ServiceGuard A.11.16.00 ServiceGuard
hpap091#

Thanks for pointing out that, and it works fine.
Sandman!
Honored Contributor

Re: awk command question: equivalent to grep.?

imho since you know both product names you don't need to grep or awk for either of them. Instead the command below will suffice for both.

# swlist -l product ServiceGuard -l product PHSS_34504
Rasheed Tamton
Honored Contributor

Re: awk command question: equivalent to grep.?

Hello,

If you are still looking the awk option, here it is:

# swlist -l product | awk ' { tolower($1) } /DigitalVideo|PHKL_8377/'
DigitalVideo B.10.20 Digital Video Software
PHKL_8377 B.10.00.00.AA vmtrace and malloc() patch

The above is from an old machine, I have. For your case it is like:

swlist -l product | awk ' { tolower($1) } /ServiceGuard|PHSS_9397/'

Regards,
Rasheed Tamton.
Dennis Handly
Acclaimed Contributor

Re: awk command question: equivalent to grep.?

# swlist -l product | awk '{ tolower($1) } /DigitalVideo|PHKL_8377/'

This doesn't really work. The result of tolower isn't used. The regular expression you have, matches without any need to downshift.
Dennis Handly
Acclaimed Contributor

Re: awk command question: equivalent to grep.?

Alright, if you want to use use toupper, you can use:
$ swlist -l product | awk 'toupper($1) ~ /MAKE|GETTEXT/'
This returns:
automake 1.9.6 automake
make 3.80 make
Gettext 0.10.39.4 GNU gettext NLS library
gettext 0.12 gettext

(Your patterns must be the case of your "to*" function.)
Rasheed Tamton
Honored Contributor

Re: awk command question: equivalent to grep.?

swlist -l product|awk ' {if(tolower($1) ~ /(digital|x11|openssh)/) print } '
DigitalVideo B.10.20 Digital Video Software
X11 B.10.20 HP-UX X Window Software
X11BMS B.10.20 HP-UX BMS for X Window Software
openssh 4.3p2 openssh

The only thing to take care is that the regex should be in lower case.

So, in this case need to use /(serviceguard|phss_9397)/
both search strings should be in lower case becuase it is already converted to lower case.