Effects of grep -o when it's not available


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Effects of grep -o when it's not available
# 1  
Old 12-16-2008
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  
Old 12-16-2008
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  
Old 12-16-2008
Quote:
Originally Posted by joeyg
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 Smilie

Last edited by earnstaf; 12-16-2008 at 03:47 PM..
# 4  
Old 12-16-2008
Tools 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  
Old 12-16-2008
Quote:
Originally Posted by joeyg
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  
Old 12-16-2008
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.
# 7  
Old 12-17-2008
Quote:
Originally Posted by rubin
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.
Edit: I used your code on a single record and it worked like a charm. Is there any way I can get that same output on multiple records?
Edit2: Disregard the below... I kept your RS=' ' the same and ran the code on an entire 400 record file, and it did output different variables for each record, but with no formatting. I need it to give a newline at the end of each individual record. Ideas?

Thanks for that.. looks like the right idea. How would I implement that if I have a file with about 400 records like the one above? Each new record is started by the "acl add" line. I tried the following with no success...
Added a ! to the start of each record:
Code:
sed 's/^acl add/\!acl add/g' aclfile.txt > temp && mv temp aclfile.txt

then I used your code, but changed the RS=' ' to RS='!'... not sure if this was the right idea.

Then I just did:
Code:
echo "$name - $source $sourceburb $dest $destburb $service"

That didn't work and I didnt figure it would... is there any way to make awk spit out those variables after evaluating each record?

Last edited by earnstaf; 12-17-2008 at 10:34 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. What is on Your Mind?

Remove filter effects from downloaded videos?

Does anyone know a way to remove filter effects from videos downloaded from YouTube or elsewhere, preferably with software that runs in Windows 7? Thank you in anticipation. (1 Reply)
Discussion started by: quadphonic
1 Replies

2. Ubuntu

Ubuntu encryption and its effects

How will Full Disk encryption effect how open-source software is compiled and run using ubuntu 14.04. Are there alternatives that are recommended? Thank you :). (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Red Hat

Effects of /etc/fstab file!

Hi Folks! I accidentally overwrote in /etc/fstab file. Can you guys please tell me, what impact it would have created, when I restarted the machine(RHEL6). I executed this command : # blkid /dev/vda5 > /etc/fstab (17 Replies)
Discussion started by: nixhead
17 Replies

4. Red Hat

3d effects in rhel6

I installed rhel 6.0 in my laptop but it is not upporting 3d graphics of rhel6 and i am getting an error message "Accelerated 3d graphics not available Desktop effects require hardware 3D support." my laptop is dll studio 1558 and my graphic card is ATI Radeon 5470 with 1 gb dedicated ram. (6 Replies)
Discussion started by: nileshgupta
6 Replies

5. Red Hat

3D effects on RHEL6 OC

Hello, I just installed RHEL6 OC on my T61p. It's great! Just for fun I'd like to use some fancy cool Desktop effects. I tried to enable 3D Desktop effects from OC Welcome Center but it gave me error message: Accelerated 3D graphics is not available. Desktop effects require hardware 3D support.... (7 Replies)
Discussion started by: susja
7 Replies

6. Cybersecurity

Effects on data size during encrypted transfer

Hi There, I wonder if any one can help me. I want to transfer a file from one site to another over an encrypted link. I want to know if there will be any imcrease in the data that will travel on the link? For example, I want to transfer a 1 GB file from one site to another, using an encrypted... (4 Replies)
Discussion started by: ahmerin
4 Replies
Login or Register to Ask a Question