LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-05-2011, 08:14 AM   #1
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Rep: Reputation: Disabled
replace grep -o with sed or awk


I would like to remplace grep -o with sed or awk...

For example:

egrep -oi "Cisco|Motorola|Arris"

-o, --only-matching: Show only the part of a matching line that matches PATTERN.

-i, --ignore-case: Ignore case distinctions in both the PATTERN and the input files.
 
Old 08-05-2011, 08:19 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Good for you.
What have you tried ?.
 
Old 08-05-2011, 08:27 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sounds plausible. Good luck
 
Old 08-05-2011, 08:32 AM   #4
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Good for you.
What have you tried ?.

I have tried with

sed -e "s/.*\(Cisco\|Motorola\|Arris\).*/\1/"

But, if doesn't find a matching prints all the line. I would like if doesn't match return empty.
 
Old 08-05-2011, 08:38 AM   #5
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Exaple of what I want but with sed or awk (because solaris doesn't have the option -o, and has to work on any *NIX) :


[root@cpe-172-16-0-136:~]# echo " aaa Motorola bbbb" | egrep -oi "Cisco|Motorola|Arris"
Motorola
[root@cpe-172-16-0-136:~]# echo " aaaMotorolabbbb" | egrep -oi "Cisco|Motorola|Arris"
Motorola
[root@cpe-172-16-0-136:~]# echo " aaaMobbbb" | egrep -oi "Cisco|Motorola|Arris"
[root@cpe-172-16-0-136:~]#
 
Old 08-05-2011, 08:55 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed by default prints the pattern space - that basically means everything it sees.
You're on the right track - look at the manpage. You need "-n" to suppress the printing, but then you need to tell it to print your selected records. Takes some testing to figure it out.
 
Old 08-05-2011, 09:34 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

sed is not the way to go for this. Consider this file:
Code:
line1
line2 maTch
line3 alternate
line4 mAtch me and alternatE eol
line5 alternate me and matCh reverse
Several possible matches in one line are hard to identify via RegEx. The Solaris version of 'sed' is also very limited and probably does not have switch for case-insensitive matching.
I don't know about the limitations of awk on solaris but this one worked:
Code:
awk 'BEGIN{IGNORECASE=1}{ for (i=1;i<=NF;i++){if ($i ~ "match|alternate"){ print $i}}}' file
The bold part are your possible matches. Adjust them accordingly.

Hope this helps.
 
1 members found this post helpful.
Old 08-05-2011, 09:47 AM   #8
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Doing very well, except for one thing:

Code:
[root@cpe-172-16-0-136:~]#  echo " aaa Motorolabbbb" | awk 'BEGIN{IGNORECASE=1}{ for (i=1;i<=NF;i++){if ($i ~ "Cisco|Motorola|Arris"){ print $i}}}'
Motorolabbbb
[root@cpe-172-16-0-136:~]#
In this case I need to print just the PATTERN, it's this posible??
 
Old 08-05-2011, 09:54 AM   #9
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Anyway this does not work on Solaris:

Code:
 
[root@cpe-172-16-0-137:~]# uname
SunOS
[root@cpe-172-16-0-137:~]# echo " aaa Motorolabbbb" | awk 'BEGIN{IGNORECASE=1}{ for (i=1;i<=NF;i++){if ($i ~ "Cisco|Motorola|Arris"){ print $i}}}'
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
[root@cpe-172-16-0-137:~]#
 
Old 08-05-2011, 09:54 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by crts View Post
sed is not the way to go for this.
On the contrary, sed is (probably) a better tool for this. Works on my old OpenSolaris too.
But then so does grep -o ... :shrug:
 
Old 08-05-2011, 09:57 AM   #11
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
For Solaris most be:

Code:
 
[root@cpe-172-16-0-137:~]# uname
SunOS
[root@iway-cnr-01:/opt/iway/commands]# echo " aaa Motorolabbbb" | nawk 'BEGIN{IGNORECASE=1}{ for (i=1;i<=NF;i++){if ($i ~ "Cisco|Motorola|Arris"){ print $i}}}'
Motorolabbbb
[root@iway-cnr-01:/opt/iway/commands]#

Last edited by nicomoresi; 08-05-2011 at 09:58 AM.
 
Old 08-05-2011, 10:01 AM   #12
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
On the contrary, sed is (probably) a better tool for this. Works on my old OpenSolaris too.
But then so does grep -o ... :shrug:
Code:
[root@iway-cnr-01:/opt/iway/commands]# uname
SunOS
[root@iway-cnr-01:/opt/iway/commands]# echo " aaa Motorola bbbb" | egrep -oi "Cisco|Motorola|Arris"
egrep: illegal option -- o
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...
[root@iway-cnr-01:/opt/iway/commands]#
 
Old 08-05-2011, 10:03 AM   #13
nicomoresi
LQ Newbie
 
Registered: Aug 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
On the contrary, sed is (probably) a better tool for this. Works on my old OpenSolaris too.
But then so does grep -o ... :shrug:
syg00 Do you know how to do it with sed?
 
Old 08-05-2011, 10:06 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by syg00 View Post
On the contrary, sed is (probably) a better tool for this. Works on my old OpenSolaris too.
But then so does grep -o ... :shrug:
How would you make a case-insensitive match for the 's' command on Solaris? According to my documentation this is a GNU extension, so I assume that it is not available on Solaris. Is it possible that you have also GNU-sed installed on your Solaris?
 
Old 08-05-2011, 10:20 AM   #15
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Indeed, Solaris' standard grep sucks. (Sorry if that's offensive to any Solaris admins.)

Code:
$ uname -sr
SunOS 5.10

$ grep -o 'simple' /etc/passwd
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
-------

@nicomoresi: Just use Perl.

Code:
#!/opt/csw/bin/perl

use warnings ;

while(<>) {

  @matches = /(Cisco|Motorola|Arris)/i ;

  print "@matches\n" if(@matches)  ;

}
That will read from stdin, and print case-insensitive matches. As written, it only prints one occurrence of cisco, motorola, or arris per line. (If one or more of those could occur several times on a line, the script needs a little tweaking.)

-------

P.S. It's a good idea to post your OS / version when starting a thread! This is not even a Linux question.

-------

BTW, should you want to match more than once per line, it's a simple matter of the following. (Just tested it.)

Code:
--- match.pl    Fri Aug  5 10:26:29 2011
+++ more-match.pl       Fri Aug  5 10:26:23 2011
@@ -4,7 +4,7 @@
 
 while(<>) {
 
-  @matches = /(Cisco|Motorola|Arris)/i ;
+  @matches = /(Cisco|Motorola|Arris)/ig ;
 
   print "@matches\n" if(@matches)  ;

Last edited by anomie; 08-05-2011 at 10:27 AM. Reason: added patch.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep help or sed or awk dmchess Linux - Software 4 09-29-2010 06:53 PM
[SOLVED] Help using awk,sed and grep shakes82 Programming 34 07-07-2010 11:12 PM
[SOLVED] Awk/Sed incremental replace Euler2 Programming 5 06-30-2010 12:27 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 07:29 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration