Re: Sony CDs are not safe to play in a Windows computer



On Sat, 05 Nov 2005 00:35:49 -0500, The Wanderer wrote:

>>>> OTOH, for most purposes I'd much rather use vi than sed.
>>>
>>> I use sed almost daily and vi never. sed is great when you need to
>>> replace some stuff in many files at once.
>>
>> That's what egrep is for... ^_^
>
> Out of curiosity, how? I've often wanted to do a search-and-replace from
> the command line, and my first instinct was to use grep, but I couldn't
> figure out how to tell it to do anything but print the line on which its
> result was found.

That's all it can do. You can't do search-and-replace with any of the grep
variants.

You normally either use sed's "s/old/new/g" or the equivalent in ed.

> I do tend to find egrep more useful than grep (just
> for versatility reasons), and I've usually managed to get my replacement
> jobs done with sed (except when they involve matching or outputting
> newlines, since sed doesn't seem to like those from the command
> line...),

Multi-line matches in sed are possible but slightly more involved. Each
command operates upon the pattern space, which normally only holds one
line of input at any time. To get around this:

First, use N to append the next line of input, which makes the pattern space
contain two lines of input separated by an embedded newline, which you can
match with \n. Then perform the modifications. Once you're done, use P
to print everything up to the newline and D to delete it and start the
next cycle. E.g. the command:

sed 'N;s/hello\( \|\n\)world/foo/;P;D'

will replace all occurrences of "hello world" with "foo", where the words
can be separated by either a space or a newline.

The "N;P;<something>;D" idiom is a common technique for ignoring newlines
when processing text.

.



Relevant Pages

  • Re: Sony CDs are not safe to play in a Windows computer
    ... from the command line, and my first instinct was to use grep, but I couldn't figure out how to tell it to do anything but print the line on which its result was found. ... use P to print everything up to the newline and D ...
    (rec.arts.anime.misc)
  • Re: simpletest vs phpunit vs ...
    ... How do I use grep command in Linux and Unix like operating systems? ...
    (comp.lang.php)
  • Re: MacTeX 2007 and documentation
    ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
    (comp.text.tex)
  • Re: whats wrong with find -exec sed?
    ... Sed is a stream editor. ... sed will print out the pattern space at the end of each cycle through the script. ... and sed will only produce output when explicitly told to via the p command. ...
    (alt.os.linux)
  • Re: appending syslog output to file
    ... I cannot find a postrotate function. ... 'postrotate' is a "command" in the logrotate ... grep mystring syslog.0>>myfile ... grep $NBR numbers ...
    (uk.comp.os.linux)