如何使一个脚本在一个文件进行多次的grep的? [英] How to make a script to make multiple grep's over a file?

查看:78
本文介绍了如何使一个脚本在一个文件进行多次的grep的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个脚本,可以做到自动以下内容:

 的grep'字符串1'file.txt的| grep的字符串2| grep的STRING3... | grep的stringN

的想法是,脚本可以这样运行:

  myScript.sh file.txt的字符串1字符串2 STRING3 ... stringN

和脚本必须返回 file.txt的中包含的所有字符串的所有行。

例如,如果 file.txt的是这样的:

 的Hello World
你好世界运行
你好星球世界

和我可以做这样的grep的:

 的grep你好file.txt的| grep的世界

和我得到的:

 的Hello World
你好世界运行
你好星球世界

我想打一个脚本,使这个自动,一个未定义数量的字符串作为参数。<​​/ P>

我发现,这是很难实现这一点,因为弦线的数量可以是可变的。首先,我试图创建一个名为阵列 ARGS 这样的 myScript.sh

 #!/斌/庆典
 ARGS =($ @)

与存储的参数的目的。我知道 $ {ARGS [0]} 将是我的 file.txt的,其余都是我需要串在不同的里grep使用,但我不知道如何着手,如果这是解决问题的最佳方法。我想AP preciate有关如何编这个任何建议。


解决方案

SED 有能力这样做完全符合单个进程,并避免这些评估有心计。生成的脚本实际上很简单。

 #!/ bin / sh的
文件= $ 1
转移
printf的'\\\\%s的D \\ n?!$ @|
SED -f - $文件

我们生成一个行 SED 脚本每个EX pression的;如果EX pression不是()发现,我们删除( D )该输入行,与下一个开始。

这假设你的 SED 接受 - 作为参数传递给 -f 来从标准输入读取该脚本。这不是完全可移植的;你或许需要将生成的脚本存储在临时文件中,而不是,如果这是一个问题。

本使用作为内部正则表达式的分隔符。如果你需要一个文字的模式之一,你将需要反斜杠转义。在一般情况下,创建一个脚本,找到一个替代隔膜这是在没有搜索前pressions的或许会是可能的,但在这一点上,我想移动到合适的脚本语言(Python的将是我的preference)来代替。

I want to make a script that can do the following automatically:

 grep 'string1' file.txt | grep 'string2' | grep 'string3' ... | grep 'stringN'

The idea is that the script can be run like this:

myScript.sh file.txt string1 string2 string3 ... stringN

and the script has to return all the lines of file.txt that contain all the strings.

For instance, if file.txt looks like this:

hello world 
hello world run 
hello planet world 

And I can make a grep like this:

grep hello file.txt | grep world

and I get:

hello world 
hello world run 
hello planet world

I want to make a script that makes this automatically, with an undefined number of strings as parameters.

I found that it is hard to achieve this, since the number of strings can be variable. First, I tried to create an array called args like this in myScript.sh:

 #!/bin/bash
 args=("$@")

with the purpose of storing the arguments. I know that the ${args[0]} is going to be my file.txt and the rest are the strings that I need to use in the distinct greps, but I don't know how to proceed and if this is the best approach to solve the problem. I would appreciate any suggestion about how to program this.

解决方案

sed is capable of doing this perfectly with a single process, and avoids these eval shenanigans. The resulting script is actually quite simple.

#!/bin/sh
file=$1
shift
printf '\\?%s?!d\n' "$@" |
sed -f - "$file"

We generate a line of sed script for each expression; if the expression is not (!) found, we delete (d) this input line, and start over with the next one.

This assumes your sed accepts - as the argument to -f to read the script from standard input. This is not completely portable; you would perhaps need to store the generated script in a temporary file instead if this is a problem.

This uses ? as the internal regex separator. If you need a literal ? in one of the patterns, you will need to backslash-escape it. In the general case, creating a script which finds an alternative separator which is in none of the search expressions would perhaps be possible, but at that point, I'd move to a proper scripting language (Python would be my preference) instead.

这篇关于如何使一个脚本在一个文件进行多次的grep的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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