我正在尝试为字符串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",
答案 0 :(得分:1)
此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
。
答案 1 :(得分:0)
AWK
awk支持范围模式,该模式与从模式1到模式2的所有内容都匹配:
awk '/(aaa|bbb|ccc)/,/[xyz]/' data.txt
PYTHON
Python允许您编译正则表达式以提高速度,并且可以通过将脚本放入文件中来将其作为单个命令来调用。
import re
pattern1 = re.compile("a|b|c")
pattern2 = re.compile("x|y|z")
saw_pattern1 = False
with open("data.txt", "rb") as fin:
for line in fin:
if saw_pattern1 and pattern2.match(line):
print("do stuff")
saw_pattern1 = pattern1.match(line)