The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
VMGL brings 3-D effects to VMs iBot UNIX and Linux RSS News 0 12-05-2008 04:10 PM
Keynote 2: Which transition and build effects can be played on my computer? iBot OS X Support RSS 0 11-14-2008 12:20 AM
Final Cut Pro: "AE Effects Error: Unable to find plug-in" warning when opening some p iBot OS X Support RSS 0 11-05-2008 01:10 AM
Culture gaps and herd effects - ZDNet iBot UNIX and Linux RSS News 0 10-08-2007 05:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-16-2008
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Effects of grep -o when it's not available

I'm on a Solaris box that doesn't have -o as an option for grep (trying to return match only).

Is there any way to get this functionality without the -o option?

I've tried both sed and awk doing something like:

Code:
echo "this is a test" | awk '/test/'
or
echo "this is a test | sed '/test/p'

but those match on the whole line, just as grep does without a -o.

Ideas?

edit:
from the man page:
Quote:
-o, --only-matching
Show only the part of a matching line that matches PATTERN

Last edited by earnstaf; 12-16-2008 at 03:44 PM..
  #2 (permalink)  
Old 12-16-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question More detail

Can you provide more detail on what you are trying to do? I am not familiar with the grep -o option.
  #3 (permalink)  
Old 12-16-2008
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by joeyg View Post
Can you provide more detail on what you are trying to do? I am not familiar with the grep -o option.
grep -o would return just the match... so, if I entered:

Code:
> echo "this is a test" | grep -o test
> test
or
> echo "this is a test" | grep -o "this is"
> this is

whereas without the -o, both of the above would return the whole line "this is a test"

edit: syntax -- forgot quote

Last edited by earnstaf; 12-16-2008 at 03:47 PM..
  #4 (permalink)  
Old 12-16-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool kind of weird, but...


Code:
> testval="this is"
> echo "wow this is a test" | sed "s/$testval/~&~/" | tr "~" "\n" | grep "$testval"
this is

Is that what you are looking for?
  #5 (permalink)  
Old 12-16-2008
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by joeyg View Post
Code:
> testval="this is"
> echo "wow this is a test" | sed "s/$testval/~&~/" | tr "~" "\n" | grep "$testval"
this is

Is that what you are looking for?
That is clever, but I don't know if it works for my situation, which is this:

I have a record that looks like:
Code:
acl add name=ACL_RULE_NAME pos=400 action=allow agent=proxy authneeded=no \
dest=netgroup:destinations_ssh destburb=external nataddr= \
service=ssh source=ipaddr:1.2.3.4 sourceburb=internal \
comments='this is comments' \
lastchangedby='changed by the guy'

I want to break that down and pull out just the stuff I care about, namely name, dest, destburb, source, sourceburb and service.

I had planned on doing something like:

Code:
while read line 
do
source=`grep -o "source=.* "`
dest=`grep -o "dest=.* "`
echo "$source $dest"
done

I'm sure there is a more elegant or effective way... actually now that I'm looking at it that wont work since they records are on different lines... might have to use awk. However, I cant even get the idea in motion without grep -o.
  #6 (permalink)  
Old 12-16-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Given your sample with (n)awk you can do something like this:


Code:
$ cat file
acl add name=ACL_RULE_NAME pos=400 action=allow agent=proxy authneeded=no \
dest=netgroup:destinations_ssh destburb=external nataddr= \
service=ssh source=ipaddr:1.2.3.4 sourceburb=internal \
comments='this is comments' \
lastchangedby='changed by the guy'



Code:
eval  `nawk '/source=[^ ]*/
             /sourceburb=[^ ]*/
             /dest=[^ ]*/
             /destburb=[^ ]*/
             /name=[^ ]*/
             /service=[^ ]*/' RS=' '  file`


Output:


Code:
$ echo $source
ipaddr:1.2.3.4
$ echo $service
ssh
$ echo $name
ACL_RULE_NAME
$ echo $destburb
external
$ echo $dest
netgroup:destinations_ssh
$ echo $sourceburb
internal

The regexp in nawk considers also the case when the variables are empty, if needed.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0