logo       

Re: Post-grep-like operations: msg#00046

editors.sed.user

Subject: Re: Post-grep-like operations

On 10 Dec 2005 at 16:31, Rob Simpson wrote:

> I am wanting to select lines containing 'fruit' or 'veg',

This will select those lines. The \| represents OR:
/fruit\|veg/

This will select all the lines that do NOT contain those terms:
/fruit\|veg/!

> and then
> carry out some operation on them, say substitution X for a.
[ ...]

> I am trying to do this in a sed file:
>
> /fruit/b
> /veg/b
> d
> s/a/X/g
>
> using the grep like command in the one-liners document.

Well, you've come to the right group! (smile)

> The last expression doesn't get carried out.

because the b command branches to the END of the script, not to
the last line of the script.

> I can't find '/b' in the documentation.

That's because /b isn't a switch, it's a command. In a structure
like this:

/abc/d

the 'd' is a command that operates on the pattern /abc/. In this
case, 'd' is a real command (delete). But in a script like this:

s/ab/c/d

the 'd' is a switch that modifies the s/// command. And I should add
that there is no 'd' switch to the s/// command. Sed operates a
little like awk in that it follows a

/pattern/ command

syntax, except that sed doesn't require curly braces for every single
one-line command, like awk does.

> Any help much appreciated,

Try this:

sed '/fruit\|veg/!d; s/a/X/g' file

In detail,

/fruit\|veg/!d; # If term is not found, delete the line
s/a/X/g; # On remaining lines, do the substitution


Hope this helps.

--
Eric Pement - pemente@xxxxxxxxxxxxx



------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/dkFolB/TM
--------------------------------------------------------------------~->

--

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> To unsubscribe from this group, send an email to:
sed-users-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/







<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise