- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell Regular expressions / replacement for AW...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:01 AM
03-10-2006 02:01 AM
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=-
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:12 AM
03-10-2006 02:12 AM
Re: Shell Regular expressions / replacement for AWK?!
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:19 AM
03-10-2006 02:19 AM
Re: Shell Regular expressions / replacement for 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:45 AM
03-10-2006 02:45 AM
Re: Shell Regular expressions / replacement for AWK?!
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=-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:49 AM
03-10-2006 02:49 AM
Re: Shell Regular expressions / replacement for AWK?!
VAR=`command`
set -A W $VAR
Then to reference the seventh element-
echo ${W[6]}
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:55 AM
03-10-2006 02:55 AM
Re: Shell Regular expressions / replacement for AWK?!
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=-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:57 AM
03-10-2006 02:57 AM
Solutionmyfunc()
{
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 02:57 AM
03-10-2006 02:57 AM
Re: Shell Regular expressions / replacement for AWK?!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 03:03 AM
03-10-2006 03:03 AM
Re: Shell Regular expressions / replacement for AWK?!
Thanks again!
-=ChaZ=-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 03:03 AM
03-10-2006 03:03 AM
Re: Shell Regular expressions / replacement for AWK?!
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2006 03:05 AM
03-10-2006 03:05 AM
Re: Shell Regular expressions / replacement for AWK?!
-=ChaZ=-