Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何回显文件名?_Linux_Bash_Grep - Fatal编程技术网

Linux 如何回显文件名?

Linux 如何回显文件名?,linux,bash,grep,Linux,Bash,Grep,我正在使用以下命令搜索.docx内容: unzip -p *.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' | grep $1 unzip-p*.docx word/document.xml | sed-e的//{1,\}>//g;s/[^[:print:]\{1,\}//g'| grep$1 但是我需要包含我搜索的单词的文件名。我如何才能做到这一点?试试: find

我正在使用以下命令搜索.docx内容:

unzip -p *.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' | grep $1
unzip-p*.docx word/document.xml | sed-e的//{1,\}>//g;s/[^[:print:]\{1,\}//g'| grep$1
但是我需要包含我搜索的单词的文件名。我如何才能做到这一点?

试试:

find . -name "*your_file_name*" | xargs grep your_word | cut -d':' -f1

您可以通过
浏览文件,查看循环:

for file in *.docx; do
  unzip -p "$file" word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' | grep PATTERN && echo $file
done
用于*.docx中的文件;做
解压-p“$file”word/document.xml | sed-e的/]\{1,\}>//g;s/[^[:print:]\{1,\}//g'| grep模式和echo$文件
完成
当grep找到模式时,
&&echo$file
部分将打印文件名。

如果您使用的是GNU grep(很可能是在Linux上),您可能希望使用以下选项:

--label=
label 将实际来自标准输入的输入显示为来自文件
标签
的输入。这在实现诸如
zgrep
之类的工具时尤其有用,例如
gzip-cd foo.gz | grep--label=foo-H something
。看见 还有
-H
选项

所以你会有

for f in *.docx
do unzip -p "$f" word/document.xml \
    | sed -e "$sed_command" \
    | grep -H --label="$f" "$1"
done

使用
grep-l
代替sed+grep。。解压-p的输出是什么?如果是文件名列表,您需要
xargs
将其传递给grepy您似乎正在尝试重新创建。看看它的源头;它不是很复杂.unzip-p不是.docx的源代码file@LaveauAnderson按照tripleeeI的建议尝试
zipgrep-l
。我不知道文件名。find将递归搜索模式并将管道发送到grep。最后剪切将显示文件名。谢谢!你是真正的MVP:D