[英]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. 我一直在尝试从Mac上的文本文件中删除所有带有“小猫”一词的行。 I've been using find with xargs to do this, but it's not working.
我一直在使用find与xargs来执行此操作,但是它不起作用。 Previously I tried using sed with the modify in place option, but to no avail.
以前,我尝试将sed与就地修改选项一起使用,但无济于事。 How can I replace each file using the awk command?
如何使用awk命令替换每个文件?
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: 我不确定为什么要使用gfind,因为仅sed就足够了:
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. 在OS X上,就地编辑要求您将其与
-i(space)''
以覆盖输入文件。
如果可以使用最新版本的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. 对于不匹配
/kittens/
每一行,执行默认操作,即打印该行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.