[Novalug] Command line search and replace tool - GNU sed?

Sean McGowan sean@mcgowanet.com
Sun Sep 15 22:16:24 EDT 2013


On Sun, Sep 15, 2013 at 9:25 PM, Zachary Zebrowski
<zak.zebrowski@gmail.com>wrote:

> Weird... So, when you do a replacement in perl, this will work:
>
> my $str = 'foo';
> $str =~ s!fo!bar! ; # which replaces the / regexp with / , but the
> special character is still there...
>
> But, when you replace with \n, the \n isn't displayed.
>
> My guess is that event with perl, you need to \\ the \n in order for
> the \\ character to be displayed.
>
> man perlop and read the section titled Gory details of parsing quoted
contructs
it gets a little hairy :)


>> My one line perl isn't up there, but you can use the x modifier in perl
> >> with regular expressions and I think it will do what you want.
> >>
>
The x modifier just ignores whitespace unless it is backslashed. (I
believe)  I never really use it.


> >> On Sep 14, 2013, at 10:25 PM, John Christopher
> >> <john.christopher1100@yahoo.com> wrote:
> >>
> >> > I need a command line tool that will search and replace in a text
> file,
> >> > and will *not* do regexp translations.
> >> >
> >> > GNU sed does *almost* what I want:
> >> >
> >> >    $ cat sedscript.txt
> >> >    s/FOO/\nBAR\n/g
> >> >
> >> >    $ cat myfile.txt
> >> >    hello FOO world
> >> >
> >> >    $ sed -f sedscript.txt     myfile.txt
> >> >    hello
> >> >    BAR
> >> >     world
> >> >
> >> > In the example above, what I want is to have the following output:
> >> >
> >> >    $ sed -f sedscript.txt     myfile.txt
> >> >    hello \nBAR\n world
> >> >
> >> > I want to avoid having to change \n to \\n in sedscript.txt (it gets
> >> > difficult to read).
> >> >
> >> > Is there a way to instruct sed to *not* translate regexp
> metacharacters
> >> > (such as \n) and leave it as is?
> >> >
> >> > Is there another command line tool that will do ultra-simple search
> and
> >> > replace and *not* do regexp translations?
> >> >
> >> > Thanks for your help.
>
Not that i know of.  using the extra backslash is the most obvious answer.
in terms of readability, i would think to use the extra backslash to make
it very obvious what i was doing.  </2cents>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.firemountain.net/pipermail/novalug/attachments/20130915/185e50de/attachment.htm>


More information about the Novalug mailing list