1

I've been trying to delete all lines with the word 'kittens' from a text file on my mac. I've been using find with xargs to do this, but it's not working. Previously I tried using sed with the modify in place option, but to no avail. How can I replace each file using the awk command?

gfind -iname "dogs_kittens.txt" -type f -print0 | xargs -0 -0 -I %in <awk '!/kittens/{print}' %in > %in
shellter
  • 36,525
  • 7
  • 83
  • 90
Network Effects
  • 174
  • 1
  • 14
  • Have you tried passing an empty argument to the -i switch? It appears to be required on the Mac version of sed. See one of the comments on this answer: http://stackoverflow.com/a/5410784/153430 – Jim Lewis Oct 15 '14 at 23:44
  • change `>%in` to `>tmp && mv tmp %in –  Oct 16 '14 at 07:19

4 Answers4

2

I think that something like this would be suitable:

awk '!/kittens/' dogs_kittens.txt > tmp && mv tmp dogs_kittens.txt

For every line that doesn't match /kittens/, perform the default action, which is to print the line.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

If you're willing to try sed again it should be straightforward:

gfind -iname "dogs_kittens.txt" -type f -execdir sed -i .meow '/kittens/d' {} +
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

I'm not sure why you're using the gfind for this, as sed alone should be sufficient:

sed -i '' '/kittens/d' dogs_kittens.txt

On OS X the in-place place editing requires that you use it with -i(space)'' to overwrite the input file.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • I would have assumed it was because there were multiple files underneath the directory being searched. – John Zwinck Oct 16 '14 at 12:07
  • @JohnZwinck, Yes, although they didn't mention all files, just "from a text file". Also, `gfind` is not native to the OS X version of GNU Utils, at least not that I've seen. – l'L'l Oct 16 '14 at 22:57
0

If you can use a recent version of GNU awk it has a -i inplace option for that.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185