简体   繁体   中英

Using xargs with awk to modify files in-place (on a Mac!)

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

如果您愿意再次尝试sed ,它应该很简单:

gfind -iname "dogs_kittens.txt" -type f -execdir sed -i .meow '/kittens/d' {} +

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.

如果可以使用最新版本的GNU awk,则可以使用-i inplace选项。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM