Jos*_*ine 12 unix linux ms-word
我有一个充满MS word文件的目录结构,我必须在目录中搜索特定的字符串.到目前为止,我使用以下命令在目录中搜索文件
找 .-exec grep -li'search_string'{} \;
找 .-name'*' - print | xargs grep'search_string'
但是,此搜索不适用于MS word文件.
是否可以在Linux中的MS word文件中进行字符串搜索?
小智 16
我是一名翻译,并且几乎不知道脚本编写,但我很生气,因为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
Run Code Online (Sandbox Code Playgroud)
欢迎所有改进和建议!
这是一种使用“解压缩”将整个内容打印到标准输出,然后通过管道传输到“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
Run Code Online (Sandbox Code Playgroud)
将脚本另存为“inword”并在三个文件中搜索“wombat”:
$ ./inword wombat file1.docx file2.docx file3.docx
file2.docx
Run Code Online (Sandbox Code Playgroud)
现在您知道 file2.docx 包含“wombat”。您可以通过添加对其他 grep 选项的支持来变得更漂亮。玩得开心。