-1

Heck I just can't remember ... I recently had a cool way to use ag with sed to do find and replace. The gist was simple, something like:

ag foo -l |  ... magic here ...   sed 's/foo/bar/g'

That doesn't work, but you might just know what does. Thanks!

PS. Three cheers for the Silver Searcher.

Robert
  • 1,284
  • 13
  • 20
  • I don't know what do you want.. `-L` gives not-match filenames, what does your magic do there??? there is no "foo" in those files, you `sed 's/foo/.../'` ? – Kent Aug 03 '18 at 20:23
  • My bad, I meant `-l`. Updated in question. – Robert Aug 03 '18 at 20:24

1 Answers1

2

xargs is the magic you are looking for:

ag -l 'foo'|xargs sed -i 's/foo/abcd/g'
Kent
  • 173,042
  • 30
  • 210
  • 270
  • I get an error with your suggestion: `sed: 1: "AppMain/State.elm": invalid command code A`. This is the first file my text is found in. BTW I am on a Mac. – Robert Aug 03 '18 at 20:33
  • 1
    @Robert read my answer carefully, do copy and paste, replace the `foo` and `bar` by your values. you forgot the important `s`. if you are on a mac (bsd sed), you may need some option like `-E`, I don't know, read the man page. – Kent Aug 03 '18 at 20:44
  • 2
    @Robert Sed on macOS requires an argument to the `-i` option, see https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux – Benjamin W. Aug 03 '18 at 21:09
  • Yes, thanks Benjamin, Mac (BSD) sed _is_ a bit different. As it explains in the link you posted, one way to make it work is to use the `-i.bak` option rather than just `-i`. As it also explains, the other way is to install Gnu sed with homebrew. – Robert Aug 04 '18 at 05:53