Unix Shell - several expressions in sed?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > several expressions in sed?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author several expressions in sed?
TakeFlight

2007-08-20, 1:59 pm

sed -e '/^sas/!d' myfile

removes any lines not starting with "sas"

How do I remove any lines not starting with ("sas" OR "gnu" or "cn")
in one sed command?

Cyrus Kriticos

2007-08-20, 1:59 pm

TakeFlight wrote:
> sed -e '/^sas/!d' myfile
>
> removes any lines not starting with "sas"
>
> How do I remove any lines not starting with ("sas" OR "gnu" or "cn")
> in one sed command?


sed -n -e '/^sas/p' -e '/^gnu/p' -e '/^cn/p myfile

or

sed -n -e '/^sas/p;/^gnu/p;/^cn/p' myfile

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Cyrus Kriticos

2007-08-20, 1:59 pm

Cyrus Kriticos wrote:
> TakeFlight wrote:
>
> sed -n -e '/^sas/p' -e '/^gnu/p' -e '/^cn/p myfile
>
> or
>
> sed -n -e '/^sas/p;/^gnu/p;/^cn/p' myfile


or

sed -n '/^\(sas\|gnu\|cn\)/p' myfile

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Cyrus Kriticos

2007-08-20, 1:59 pm

Cyrus Kriticos wrote:
> TakeFlight wrote:
>
> sed -n -e '/^sas/p' -e '/^gnu/p' -e '/^cn/p myfile
>
> or
>
> sed -n -e '/^sas/p;/^gnu/p;/^cn/p' myfile


or

sed -n '/^sas\|gnu\|cn/p' myfile

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Cyrus Kriticos

2007-08-20, 1:59 pm

Cyrus Kriticos wrote:
> TakeFlight wrote:
>
> sed -n -e '/^sas/p' -e '/^gnu/p' -e '/^cn/p myfile
>
> or
>
> sed -n -e '/^sas/p;/^gnu/p;/^cn/p' myfile


or

sed -e '/^\(sas\|gnu\|cn\)/!d' myfile

or

sed -e '/^sas\|^gnu\|^cn/!d' myfile

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Chris F.A. Johnson

2007-08-20, 7:25 pm

On 2007-08-20, TakeFlight wrote:
> sed -e '/^sas/!d' myfile
>
> removes any lines not starting with "sas"
>
> How do I remove any lines not starting with ("sas" OR "gnu" or "cn")
> in one sed command?


sed -n -e '/^sas/p' -e '/^gnu/p' -e '/^cn/p' myfile

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Cyrus Kriticos

2007-08-20, 7:25 pm

Cyrus Kriticos wrote:
> Cyrus Kriticos wrote:

I forgot the last "'" before "myfile".
[vbcol=seagreen]
>
> or
>
> sed -e '/^\(sas\|gnu\|cn\)/!d' myfile
>
> or
>
> sed -e '/^sas\|^gnu\|^cn/!d' myfile


or

sed -n '/^sas\|^gnu\|^cn/p' myfile

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Ed Morton

2007-08-21, 1:34 am

TakeFlight wrote:
> sed -e '/^sas/!d' myfile
>
> removes any lines not starting with "sas"


That's double negative logic. Why not say "select any lines starting
with sas", then the correct solution becomes clearer:

grep '^sas' myfile

> How do I remove any lines not starting with ("sas" OR "gnu" or "cn")
> in one sed command?
>


sed is the wrong tool for the job. What you mean to say is "select any
lines starting with ("sas" OR "gnu" or "cn")" and grep is again the
right tool to use:

grep -E '^(sas|gnu|cn)' file

Regards,

Ed.
Michael Tosch

2007-08-27, 7:23 am

TakeFlight wrote:
> sed -e '/^sas/!d' myfile
>
> removes any lines not starting with "sas"
>
> How do I remove any lines not starting with ("sas" OR "gnu" or "cn")
> in one sed command?
>


sed -n '/^sas/p;/^gnu/p;/^cn/p'

egrep '^sas|^gnu|^cn'

awk '/^sas|^gnu|^cn/'


--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com