gpt4 book ai didi

linux - 复制包含单词但不包含其他单词的文件。/grep 不使用 for 循环

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:12 26 4
gpt4 key购买 nike

我是 Linux 新手,在尝试使用管道 grep 或查找命令时卡住了。我需要找到一个文件:

  • 名称模式request_q_t.xml
  • 包含“短语 1”
  • 不包含“word 2” 将其复制到特定位置。

我尝试了 pipe grep 命令来定位文件而不是复制。

for filename in $(grep --include=request_q*_t*.xml -li '"phrase 1"' $d/ | xargs grep -L '"word 2"')
do
echo "coping file: '$filename'"
cp $filename $outputpath
filefound=true
done

当我在命令行中尝试这个 grep 命令时它工作正常

grep --include=request_q*_t*.xml -li '"phrase 1"' $d/ | xargs grep -L '"word 2"'

但是我在 for 循环中遇到错误。由于某种原因,grep 命令的输出是

(Standard Input)
(Standard Input)
(Standard Input)
(Standard Input)
  1. 我不确定我做错了什么。
  2. 执行此操作的有效方法是什么.. 它是一个巨大的文件系统,我必须在其中搜索。

最佳答案

find . -name "request_q*_t*.xml" -exec sh -c "if grep -q phrase\ 1 {} && ! grep -q word\ 2 {} ;then cp {} /path/to/somewhere/;fi;" \;

关于linux - 复制包含单词但不包含其他单词的文件。/grep 不使用 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51606058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com