我正在尝试为字符串 a、b 和 c 搜索 3 个字段。我知道这可以用
grep -E 'a|b|c'
但是,我还想对字符串 x、y 和 z 进行 grep,包括以下行。我知道这可以用
grep -A1 'x'
所以我的问题是,是否可以将所有这些组合到一个命令中?例如。类似的东西(我知道这个命令不起作用,只是一个例子)
grep -E 'a|b|c' -A1 'x|y|z'
如果有更好的方法而不使用 grep,甚至使用 python 会有帮助,我只是求助于使用 grep,因为我认为它比使用 python 逐行读取文件更快。干杯!
编辑: 所以我有一个包含重复部分的大文件,它看起来像这样:
{
"source_name": [
"$name"
],
"source_line": [
52
],
"source_column": [
1161
],
"source_file": [
"/somerandomfile"
],
"sink_name": "fwrite",
"sink_line": 55,
"sink_column": 1290,
"sink_file": "/somerandomfile",
"vuln_name": "vuln",
"vuln_cwe": "CWE_862",
"vuln_id": "17d99d109da8d533428f61c430d19054c745917d0300b8f83db4381b8d649d83",
"vuln_type": "taint-style"
}
{} 之间的这一部分在文件中重复。所以我试图 grep 的是 source_name、source_line 和 source_file 下面的行以及 vuln_name、sink_file 和 sink_line。所以样本输出应该是:
"source_name": [
"$name"
"source_line": [
52
"source_file": [
"/somerandomfile"
"sink_line": 55,
"sink_file": "/somerandomfile",
"vuln_name": "vuln",
最佳答案
这个 python 脚本应该能够完成这项工作,它允许进行一些难以进入密集 grep 命令的临时定制:
my_grep.py
import re
import sys
first = re.compile(sys.argv[1])
second = re.compile(sys.argv[2])
with open(sys.argv[3]) as f:
content = f.readlines()
for idx in range(len(content)):
first_match = first.search(content[idx])
if first_match:
print(content[idx])
second_match = second.search(content[idx])
if second_match and (idx+1) < len(content):
print(content[idx])
print(content[idx+1])
您可以像这样生成您想要的输出:
python my_grep.py 'sink_line|sink_file|vuln_name' 'source_name|source_line|source_file' input_file
鉴于您的输入文件名为 input_file
。
关于python - Grep for multiple strings 和多个字符串包括以下行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395317/