Ack for find & replace?

551 views
Skip to first unread message

David Mankin

unread,
Aug 21, 2012, 8:50:10 PM8/21/12
to ack-...@googlegroups.com
Hello fellow Ack users,

Please help me with my ack+bash fu or point out a tool I'm missing:

I have an ack command line that's highlighting exactly the text I want to find & replace.  How can I have perl or sed fix my files instead of having to edit each file by hand?

$ ack --java FooClass
my/package/BarClass.java:16 import my.other.package.FooClass;

I want something like ack --inplace --replacement="NewNameClass" FooClass

I looked on betterthangrep.com/documentation and /more-tools to no avail but it's a part of some tool on more-tools that I didn't notice.

Thanks,
David

Bill Ricker

unread,
Aug 21, 2012, 9:21:12 PM8/21/12
to ack-...@googlegroups.com
In-place edits is not a direct Ack use-case. (Do we want to add it for Ack 2?) 

There is a common Perl idiom for this, however, that can be combined with ack thusly :

perl -i.bak -e 's/\bFooClass\b/NewNameParent/g' $(ack -l --java FooClass) 

the pair of \b require matching whole words so you don't accidentally change BarFooClassics to BarNewNameParentics (like the famed "Personal electronic deDeputys")

ack -l in subcommand here finds matching java source files only, ignoring ignored stuff as usual, so you get Ack's intelligence. 

-i.bak says in-place edit, save original as $filename.bak

-- bill


--
You received this message because you are subscribed to the Google Groups "ack dev/users" group.
To post to this group, send email to ack-...@googlegroups.com.
To unsubscribe from this group, send email to ack-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ack-users?hl=en.



--
Bill
@n1vux bill....@gmail.com

Andy Lester

unread,
Aug 21, 2012, 9:24:31 PM8/21/12
to ack-...@googlegroups.com

On Aug 21, 2012, at 8:21 PM, Bill Ricker <bill....@gmail.com> wrote:

> In-place edits is not a direct Ack use-case. (Do we want to add it for Ack 2?)

No, ack will always be read-only. It's even got its own FAQ entry.

Wouldn’t it be great if ack did search & replace?

No, ack will always be read‐only. Perl has a perfectly good way to do
search & replace in files, using the "−i", "−p" and "−n" switches.

You can certainly use ack to select your files to update. For example,
to change all "foo" to "bar" in all PHP files, you can do this from the
Unix shell:

$ perl −i −p −e's/foo/bar/g' $(ack −f −−php)


The FAQ oughta go on the docs page at some point.



xoa

--
Andy Lester => an...@petdance.com => www.petdance.com => AIM:petdance

u...@sysarch.com

unread,
Aug 21, 2012, 10:01:13 PM8/21/12
to ack-...@googlegroups.com
On 08/21/2012 09:21 PM, Bill Ricker wrote:
> In-place edits is not a direct Ack use-case. (Do we want to add it for Ack
> 2?)
>
> There is a common Perl idiom for this, however, that can be combined with
> ack thusly :
>
> perl -i.bak -e 's/\bFooClass\b/NewNameParent/g' $(ack -l --java FooClass)
>
> the pair of \b require matching whole words so you don't accidentally
> change BarFooClassics to BarNewNameParentics (like the famed "Personal
> electronic deDeputys")
>
> ack -l in subcommand here finds matching java source files only, ignoring
> ignored stuff as usual, so you get Ack's intelligence.
>
> -i.bak says in-place edit, save original as $filename.bak

and File::Slurp has the edit_file and edit_file_lines subs which can do
in-place edits of your files. you can pass your ack results on the
command line or in backticks and have more control over the edit in
place. -i is good for command line stuff but the internal version is
painful.

uri



Bill Ricker

unread,
Aug 21, 2012, 10:10:42 PM8/21/12
to ack-...@googlegroups.com

On Tue, Aug 21, 2012 at 10:01 PM, <u...@sysarch.com> wrote:
and File::Slurp has the edit_file and edit_file_lines subs which can do
in-place edits of your files. you can pass your ack results on the
command line or in backticks and have more control over the edit in
place. -i is good for command line stuff but the internal version is
painful.

Agreed if i were doing it deep in a larger script, your File::Slurp::edit_files is nicer. Since the FAQ request is for Ack-like commandline sugar, something even simpler than -i seems useful (as a RoboEdit competitor). 

A find_replace.pl that used File::Slurp::edit_file and App::Ack's core might be a good accessory and demo for both. 

--
Bill
@n1vux bill....@gmail.com

u...@sysarch.com

unread,
Aug 21, 2012, 11:46:55 PM8/21/12
to ack-...@googlegroups.com
i have in my update queue a simple shortcut name module for file::slurp
that would support short oneliners. it would allow something like this:

perl -MFS=ef -e 'ef { s/foo/bar/g } $_ for @ARGV' $(ack ....)

the FS module would come with the File::Slurp distro but would have to
be installed by hand. i learned of this trick from another module which
asks the user if they want the shortcut module installed and i don't
want to go that far.

uri
Reply all
Reply to author
Forward
0 new messages