查找并grep返回不带字符串的文件 [英] Find and grep to return files without string

查看:207
本文介绍了查找并grep返回不带字符串的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下find,sed和grep命令.

I have the following find, sed, and grep command.

 find . -type d \( -name ThirdParty -o -name 3rdParty -o -name 3rd_party \) -prune -o -type f \( -name "*.bat" \) -exec grep -L 'FOO' {} \; -print0 | xargs -0 sed -i -e '1iFOO'

我希望能够在目录中找到所有不包含字符串"FOO"的.bat文件,并将它们通过管道发送到sed命令,该命令将字符串"FOO"添加到文件顶部.但是,当我运行命令的find和grep部分(不使用sed)时:

I want to be able to find all .bat files in a directory that do NOT contain the string "FOO", and pipe them to a sed command that adds the string "FOO" to the top of the file. However, when i run the find and grep portion of my command (Without sed):

 find . -type d \( -name ThirdParty -o -name 3rdParty -o -name 3rd_party \) -prune -o -type f \( -name "*.bat" \) -exec grep -L 'FOO' {} \; -print

它返回所有bat文件,甚至包含字符串'FOO'的文件.这使我相信grep命令是错误的.我怎样才能解决这个问题?谢谢

it returns ALL bat files, even ones containing the string 'FOO'. This leads me to believe that the grep command is faulty. how can i fix this? thank you

推荐答案

更改:

grep -L 'FOO'

收件人:

awk '/FOO/{exit 1}'

因此,只有在文件中不存在FOO的情况下,您才可以打印文件名.

so you only get the file name printed if FOO is not present in the file.

为清楚起见,整个命令行将是:

To be clear the whole command line would then be:

find . -type d \( -name ThirdParty -o -name 3rdParty -o -name 3rd_party \) \
    -prune -o -type f \( -name "*.bat" \) \
    -exec awk '/FOO/{exit 1}' {} \; -print0 |
        xargs -0 sed -i -e '1iFOO'

这篇关于查找并grep返回不带字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆