如何回显文件名?

时间:2017-03-21 07:07:41

标签: linux bash grep

我使用此命令在.docx内容中搜索:

unzip -p *.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' | grep $1

但我需要包含我搜索过的单词的文件名。我该怎么办?

3 个答案:

答案 0 :(得分:0)

尝试:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char course; //this decl stores only single character not string

    printf("Enter Your Course Name: \n");
    scanf(" %s", &course); //passing string argument 

    if (course == 'TOEFL') {//can't compare strings with == use strcmp
        printf("Yes, you are eligible \n");
    } else {
        printf("You Can Not Join Us \n");
    }

    return 0;
}

答案 1 :(得分:0)

您可以通过for周期浏览文件:

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

当grep找到模式时,&& echo $file部分会打印文件名。

答案 2 :(得分:0)

如果你正在使用GNU grep(可能就像你在Linux上一样),你可能想要使用这个选项:

  

--label= 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
相关问题