Operating System - HP-UX
1846087 Members
4041 Online
110253 Solutions
New Discussion

Re: Shell Regular expressions / replacement for AWK?!

 
SOLVED
Go to solution
Charles Harris
Super Advisor

Shell Regular expressions / replacement for AWK?!

Hi All,

I'm writting a script that has to be very light. To this end I'm looking for a shell regexp master to replace the common use of awk in the example below...

command | awk 'NF == 7{print $7}'

with something like:

VAR=`command`
echo ${VAR##/*}

or the like....

Any pointers warmly received as ever!

Cheers,

-=ChaZ=-
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: Shell Regular expressions / replacement for AWK?!

Hi Charles:

Well, the 'awk' example you gave could be shortened to:

# command | awk '{print NF}'

The shell as very little regular expression support as you know. Posting a bit more of the context and goals of the script would be helpful in offering suggestions.

Regards!

...JRF...
Mark Greene_1
Honored Contributor

Re: Shell Regular expressions / replacement for AWK?!

This depends on what you mean by "light". You can substitute this for the awk:

command|tr -s " " |cut -d" " -f7

except that spawns two child processes instead of just one, but both cut and tr are about half the size of awk, so you're taking about the same memory footprint.

mark
the future will be a lot like now, only later
Charles Harris
Super Advisor

Re: Shell Regular expressions / replacement for AWK?!

Hello,

Thanks for the comments, I suppose I should have mentioned a little more detail... ;-P

I'm trying to process a string in the lightest possible fashion, minimum processes + CPU hit as the target system is a network controller. The script is a status monitor and may well get executed a number of times per second....

The output I'm trying to parse is pretty straight forward, along the lines of:

component a has a session status enable
component b has a session status enable
component c has a session status disable

I just wanted to see if I could get the shell to strip out only the enable / disable msg without running to many external commands...

Thanks again!

-=ChaZ=-
Rodney Hills
Honored Contributor

Re: Shell Regular expressions / replacement for AWK?!

Use arrays which are available in both posix sh or ksh-

VAR=`command`
set -A W $VAR

Then to reference the seventh element-
echo ${W[6]}

HTH

-- Rod Hills
There be dragons...
Charles Harris
Super Advisor

Re: Shell Regular expressions / replacement for AWK?!

Rodney,

That's a very good point! - any ideas for stripping out the extra lines (if forgot to include them in the above) ie:

la la la la enable
la la la la al al
la la la la disable

Thanks again!

-=ChaZ=-
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Shell Regular expressions / replacement for AWK?!

Something like this should work leveraging the set coomand inside a function:

myfunc()
{
typeset X=$(command)
set ${X}
echo "${7}"
return 0
} # myfunc

VAR=$(myfunc)
echo "Var = ${VAR}"

I would probably add some more robust testing but that's the gist of it.
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Shell Regular expressions / replacement for AWK?!

You could do a test-

if [[ ${W[5] eq "status" ]] ; then
if [[ ${W[6] eq "enable" ]] ; then
... whatever for enable...
elsif [[ ${W[6] eq "disable" ]] ; then
... whatever for disable...
fi
fi

PS- To get the number of elements in the array do-
echo ${#W[*]}

HTH

-- Rod Hills
There be dragons...
Charles Harris
Super Advisor

Re: Shell Regular expressions / replacement for AWK?!

Thanks gents! - I think I've got it down now. I've just tested a script based on both themes and the results are very good (super quick)!!

Thanks again!

-=ChaZ=-
James R. Ferguson
Acclaimed Contributor

Re: Shell Regular expressions / replacement for AWK?!

Hi (again) Charles:

If you have a fixed number of fields in each "line" you could you the shell's read to split the line into fields which you can then test. By example:

while read a b c d e f VAR
do
[ "${VAR}" = "enable" ] && echo ${VAR}
done < file

Regards!

...JRF...
Charles Harris
Super Advisor

Re: Shell Regular expressions / replacement for AWK?!

Good work all & thanks again!!!

-=ChaZ=-