我有一个满是MS word文件的目录结构,我必须在目录中搜索特定的字符串。到目前为止,我一直使用以下命令在目录中搜索文件
找出来。-exec grep -li 'search_string‘{}; 找出来。-name '*‘-print \ xargs grep 'search_string’
但是,这个搜索并不适用于MS word文件。
在Linux中可以在MS word文件中进行字符串搜索吗?
发布于 2013-01-26 14:15:06
我是一个翻译家,对脚本几乎一无所知,但我对grep无法扫描Word .doc文件感到非常生气,因此我想出了如何使这个小shell脚本使用catdoc和grep搜索.doc文件目录中的给定输入字符串。
您需要安装catdoc
和docx2txt
软件包
#!/bin/bash
echo -e "\n
Welcome to scandocs. This will search .doc AND .docx files in this directory for a given string. \n
Type in the text string you want to find... \n"
read response
find . -name "*.doc" |
while read i; do catdoc "$i" |
grep --color=auto -iH --label="$i" "$response"; done
find . -name "*.docx" |
while read i; do docx2txt < "$i" |
grep --color=auto -iH --label="$i" "$response"; done
欢迎所有改进和建议!
发布于 2014-06-12 01:26:31
这里有一种方法可以使用"unzip“将整个内容打印到标准输出,然后通过管道传输到"grep -q”,以检测输出中是否存在所需的字符串。它适用于docx格式文件。
#!/bin/bash
PROG=`basename $0`
if [ $# -eq 0 ]
then
echo "Usage: $PROG string file.docx [file.docx...]"
exit 1
fi
findme="$1"
shift
for file in $@
do
unzip -p "$file" | grep -q "$findme"
[ $? -eq 0 ] && echo "$file"
done
将脚本保存为"inword“,并在以下三个文件中搜索"wombat”:
$ ./inword wombat file1.docx file2.docx file3.docx
file2.docx
现在您知道file2.docx包含了"wombat“。您可以通过添加对其他grep选项的支持来获得更好的效果。玩得开心。
发布于 2012-11-29 06:28:29
more的最新版本在文本的每个字母之间穿插在我还无法理解的内容之间。我编写了自己的MS Word搜索实用程序,在搜索字段中的每个字符之间插入ascii,它工作得很好。笨手笨脚但没问题。还有很多问题。也许垃圾字符并不总是一样的。还需要做更多的测试。如果有人能编写一个考虑到所有这些的实用程序,那就太好了。在我的windows机器上,相同的文件对搜索有很好的响应。我们能做到的!
https://stackoverflow.com/questions/11462184
复制相似问题