LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 09: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, 09:19 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

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

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Sounds plausible. Good luck
 
Old 08-05-2011, 09: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, 09: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, 09:55 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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, 10:34 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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, 10: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, 10: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, 10:54 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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, 10: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 10:58 AM.
 
Old 08-05-2011, 11: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, 11: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, 11:06 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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, 11: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 11:27 AM. Reason: added patch.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:53 PM
[SOLVED] Help using awk,sed and grep shakes82 Programming 34 07-08-2010 12:12 AM
[SOLVED] Awk/Sed incremental replace Euler2 Programming 5 06-30-2010 01:27 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 08:29 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM


All times are GMT -5. The time now is 02:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration