grep -f压缩文件夹中的文件 [英] grep -f on files in a zipped folder

查看:233
本文介绍了grep -f压缩文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我希望有人能够帮助......



我正在执行递归的fgrep / grep -f搜索文件夹在我的一个程序中使用以下命令:

我正在使用的命令

  grep -r -i -z -I -f / path / to / pattern / file /home/folder/TestZipFolder.zip 



模式文件内部是我试图搜索的字符串Dog。



压缩文件夹中有许多包含字符串Dog的文本文件。



grep -f命令成功找到包含字符串Dog的文本文件在压缩文件夹内的3个文件中,但它将输出全部打印在一行上,并在结尾处出现一些奇怪的字符,即PK(如下所示)。当我尝试将输出打印到我的程序中的某个文件时,其他字符出现在最后,例如 ^ B ^ T ^ @



grep -f命令的输出:

  TestZipFolder / test.txtThis是包含字符串DogPKtest1.txtDog的文件,在此文件中再次找到.PKTestZipFolder / another.txtDog写入此文件中.PK 

我怎样才能得到每一个字符串Dog被发现在一个新行上打印的文件,这样他们就不会像现在一样在一行上被组合在一起?
输出中出现PK和其他奇怪字符的位置,以及如何防止它们出现?


希望的输出

  TestZipFolder / test.txt:这是一个包含字符串的文件Dog 
TestZipFolder / test1.txt:Dog,在这个文件中被再次发现
TestZipFolder / another.txt:Dog被写入这个文件

沿着这些线的某些事情,用户可以看到文件中的字符串可以找到的位置如果您对非zip文件的文件运行grep命令,则以这种格式输出)。



您对此的帮助非常感谢,谢谢

解决方案

如果您需要多行输出,最好使用 zipgrep

  zipgrep -spatternTestZipFolder.zip 

-s 用于抑制错误消息(可选)。该命令将打印每个匹配的行以及文件名。如果要删除重复名称,当文件中有多个匹配项时,必须使用loops / grep或awk或sed完成其他一些处理。



实际上, zipgrep egrep unzip 的组合。它的用法如下:

  zipgrep [egrep_options]模式文件[.zip] [文件(s)...] [-x xfile(s)...] 

所以你可以传递任何egrep选项。

I have a problem I am hoping someone will be able to help with...

I am performing a recursive fgrep/grep -f search on a zipped up folder using the following command in one of my programs:

The command I am using

grep -r -i -z -I -f /path/to/pattern/file /home/folder/TestZipFolder.zip

Inside the pattern file is the string "Dog" that I am trying to search for.

In the zipped up folder there are a number of text files containing the string "Dog".

The grep -f command successfully finds the text files containing the string "Dog" in 3 files inside the zipped up folder, but it prints the output all on one line and some strange characters appear at the end i.e PK (as shown below). And when I try and print the output to a file in my program other characters appear on the end such as ^B^T^@

Output from the grep -f command:

TestZipFolder/test.txtThis is a file containing the string DogPKtest1.txtDog, is found again in this file.PKTestZipFolder/another.txtDog is written in this file.PK 

How would I get each of the files where the string "Dog" has been found to print on a new line so they are not all grouped together on one line like they are now? Also where are the "PK" and other strange characters appearing from in the output and how do i prevent them from appearing?

Desired output

TestZipFolder/test.txt:This is a file containing the string Dog
TestZipFolder/test1.txt:Dog, is found again in this file
TestZipFolder/another.txt:Dog is written in this file

Something along these lines, whereby the user is able to see where the string can be found in the file (you actually get the output in this format if you run the grep command on a file that is not a zip file).

your help with this is much appreciated, thanks

解决方案

If you need a multiline output, better use zipgrep :

zipgrep -s "pattern" TestZipFolder.zip

the -s is to suppress error messages(optional). This command will print every matched lines along with the file name. If you want to remove the duplicate names, when more than one match is in a file, some other processing must be done using loops/grep or awk or sed.

Actually, zipgrep is a combination egrep and unzip. And its usage is as follows :

zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...]

so you can pass any egrep options to it.

这篇关于grep -f压缩文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆