复杂的awk遍历CSV [英] Complicated awk loop over CSV

查看:204
本文介绍了复杂的awk遍历CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个CSV同一种类型的规则库的,我需要解析和创建命令,我有多个问题。

我开始之前,这里是它是如何布局的例子,它是什么样子:

  $ 1 =规则数
$ 3 =来源
$ 4 =目的地
$ 5 =服务
$ 6 =行动
$ J =轨道
$ 10 =评论

 安全策略:Blahblahblah ,,,,,,,,,
12日,host_A,净-B,HTTPS,下降,日志,任何,任何评论
13日,host_A,净-B,SMTP,下降,日志,任何,任何评论
14日,host_A,净-B,HTTP,接受日志,任何,任何评论
,,净-C ,,,,,,,
,,净-D ,,,,,,,
15日,host_A,净-B,HTTP,接受日志,任何,任何评论
,, host_B,净-C,service_X ,,,,,
,, host_C,净-D,service_y ,,,,,
,, host_D ,,,,,,,
,, host_E ,,,,,,,

问题1:第1列(规则号)需要在循环内进行调整。我需要从中减去一个变量等于正确的号码(需转移)。例如,第一规则#12,需要成为#1的环中。

我使用它来创建我需要从原始对于每个连续的线中减去变量(取第一行中,由一个减法):

 `awk的-F,'NR == 2 {$打印1 -1}

问题2:我需要在规则#每个实例来遍历这个循环。 IE浏览器:每个规则可以有多个源/目标/服务,我需要能够将新对象与正确的规则链接

错误检查$ 1还需要做,因为有一些字段/这就需要跳过它与禁用或类似的东西开始的规则。这似乎这样的伎俩:

 的awk -F,'$ 1〜^ [0-9] * ${$打印1}

总之,想我最终输出看起来像下面这样:

(所有回显的/ AWK print'd等):

 如果新规则#被发现在$ 1:
创建规则security_rule
创建操作$ rule_number $行动
创建评论$ rule_number $评论
创建源$ rule_number $源< ---根据需要重复多次
创建目标$ rule_number $目的地<关于要求---迭代多次
创建服务$ rule_number $服务< ---迭代多次要求
创建轨道$ rule_number $轨道

等等......

任何帮助/建议你可以做将AP preciated。

感谢您,

编辑:一个更好的例子(第1 = CSV第12条 - 这仍然是粗糙的打印语句,我可以在合适的打印值后填写):

 如果新规则#被发现在$ 1:
创建规则security_rule
创建操作规则1滴
创建注释规则1这是一个注释
创建来源规则1 host_A
创建目标规则1净-B
创建服务规则1 HTTPS
创建跟踪规则1登录

与多个源/目的/服务的那些只会增加额外的创建源第X条线,像这样:

 如果新规则#被发现在$ 1:
创建规则security_rule
创建操作规则3接受
创建评论第3条这是一个注释
创建来源规则3 host_A
创建来源规则3网-C
创建源治网3-D
创建目标规则3网-B
创建服务规则3 HTTP
创建跟踪规则3登录


解决方案

awk中能做到这一点,但它是一个触摸笨重。你基本上收集一个大的字符串信息,然后打印出来当你与每个人完成。 (只记得打印的最后一个太)

我省略了如果新规则#被发现在$ 1:位......因为我并不完全明白应该如何工作。如果你绝对需要跟踪线在年底现身...只是复制为$ 3,$ 4及5 $ 7 $行。

  BEGIN {
    FS =,; RECNUM = 0; curLine =
}$ 1〜/ ^安全策略/ {}旁边$ 1!={
    打印curLine,\\ n
    RECNUM ++;
    $ 1 = RECNUM;
    curLine = sprintf的(创建规则security_rule \\ n创建行动规则%d个%S \\ n,$ 1,$ 6);
    curLine = curLine的sprintf(创建注释规则%d个\\%s \\的\\ n,$ 1,$ 10);
    curLine = curLine的sprintf(创建跟踪规则,%d个%S \\ n,$ 1,$ 7);
}
$ 1 =={
    $ 1 = RECNUM;
}$ 3!={
    curLine = curLine的sprintf(创源统治%d个%S \\ n,$ 1,$ 3);
}
$ 4个!={
    curLine = curLine的sprintf(创建目标规则%d个%S \\ n,$ 1,$ 4);
}
$!5 ={
    curLine = curLine的sprintf(创建服务规则%d个%S \\ n,$ 1,$ 5);
}
END {打印curLine}

有关你上面的投入,这给了我:

 创建规则security_rule
创建操作规则1滴
创建注释第1条意见
创建跟踪规则1登录
创建来源规则1 host_A
创建目标规则1净-B
创建服务规则1 HTTPS
创建规则security_rule
创建操作规则2降
创建注释第2条意见
创建跟踪规则2登录
创建来源规则2 host_A
创建目标规则2净-B
创建服务规则2 SMTP
创建规则security_rule
创建操作规则3接受
创建评论第3条意见
创建跟踪规则3登录
创建来源规则3 host_A
创建目标规则3网-B
创建服务规则3 HTTP
创建来源规则3网-C
创建源治网3-D
创建规则security_rule
创建操作规则4接受
创建评论第4条意见
创建跟踪规则4日志
创建来源规则4 host_A
创建目标规则4净-B
创建服务规则4 HTTP
创建来源规则4 host_B
创建目标规则4净-C
创建服务规则4 service_X
创建来源规则4 host_C
创建目标规则4净-D
创建服务规则4 service_y
创建来源规则4 host_D
创建来源规则4 host_E

I've got multiple CSV's with a type of rulebase that I need to parse and create commands from, and I'm having multiple issues.

Before I get started, here's an example of how it's laid out, and what it looks like:

$1 = Rule number
$3 = Source
$4 = Destination
$5 = Service
$6 = Action
$7 = Track
$10 = Comments

.

Security Policy: Blahblahblah,,,,,,,,,
12,,host_A,net-B,https,drop,Log,Any,Any,comments
13,,host_A,net-B,smtp,drop,Log,Any,Any,comments
14,,host_A,net-B,http,accept,Log,Any,Any,comments 
,,net-C,,,,,,,
,,net-D,,,,,,,
15,,host_A,net-B,http,accept,Log,Any,Any,comments
,,host_B,net-C,service_X,,,,,
,,host_C,net-D,service_y,,,,,
,,host_D,,,,,,,
,,host_E,,,,,,,

Problem #1: Column 1 (Rule Number) needs to be adjusted within the loop. I need to subtract a variable from it to equal the correct number (needs to shift). For example, the first rule #12, needs to become #1 within the loop.

I use this to create the variable I need to subtract from the original for each successive line (take the first line, subtract by one):

`awk -F, 'NR==2 {print $1 -1 }'

Problem #2: I need to iterate this loop over every instance of Rule#. IE: Each rule "can" have multiple sources/destinations/services, and I need to be able to link the new objects with the correct rule.

Error checking for $1 also needs to be done, as there are some fields/rules which need to be skipped which start with 'disabled' or something similar. This seems to do the trick:

awk -F, '$1 ~ "^[0-9]*$" {print $1}

Overall, I'd like the final output to look something like the following:

(all echo'd/awk print'd etc):

if new rule # is found in $1:
create rule security_rule
create action $rule_number $action
create comment $rule_number $comment
create source $rule_number $source <--- iterate as many times as required
create destination $rule_number $destination <--- iterate as many times as required
create service $rule_number $service <--- iterate as many times as required
create track $rule_number $track

etc...

Any help/suggestions you could make would be appreciated.

Thank you,

Edit: A better example (Rule 1 = rule 12 in CSV - these are still rough print statements, I can fill in the right print values later):

if new rule # is found in $1:
create rule security_rule
create action rule 1 drop
create comment rule 1 "This is a comment"
create source rule 1 host_A
create destination rule 1 net-B
create service rule 1 https
create track rule 1 Log

The ones with multiple source/destination/service would simply add extra 'create source rule x' lines, like so:

if new rule # is found in $1:
create rule security_rule
create action rule 3 accept
create comment rule 3 "This is a comment"
create source rule 3 host_A
create source rule 3 net-C
create source rule 3 net-D
create destination rule 3 net-B
create service rule 3 http
create track rule 3 Log

解决方案

Awk can do this, but it's a touch unwieldy. You basically collect the information in one big string and then print it out when you're done with each one. (just remember to print the last one too)

I've omitted the if new rule # is found in $1: bit... because I don't entirely understand how that is supposed to work. If you absolutely need the "track" line to show up at the end... just duplicate the lines for $3,$4 and $5 for $7.

BEGIN{
    FS=",";recNum=0;curLine=""
}

$1 ~ /^Security Policy/ {next}

$1!="" {
    print curLine,"\n"
    recNum++;
    $1=recNum;
    curLine=sprintf("create rule security_rule\ncreate action rule %d %s\n",$1,$6);
    curLine=curLine sprintf("create comment rule %d \"%s\"\n",$1,$10);
    curLine=curLine sprintf("create track rule %d %s\n",$1,$7);
}
$1=="" {
    $1=recNum;
}

$3!=""{
    curLine=curLine sprintf("create source rule %d %s\n",$1,$3);
}
$4!=""{
    curLine=curLine sprintf("create destination rule %d %s\n",$1,$4);
}
$5!=""{
    curLine=curLine sprintf("create service rule %d %s\n",$1,$5);
}
END {print curLine}

For your input above, that gives me:

create rule security_rule
create action rule 1 drop
create comment rule 1 "comments"
create track rule 1 Log
create source rule 1 host_A
create destination rule 1 net-B
create service rule 1 https


create rule security_rule
create action rule 2 drop
create comment rule 2 "comments"
create track rule 2 Log
create source rule 2 host_A
create destination rule 2 net-B
create service rule 2 smtp


create rule security_rule
create action rule 3 accept
create comment rule 3 "comments"
create track rule 3 Log
create source rule 3 host_A
create destination rule 3 net-B
create service rule 3 http
create source rule 3 net-C
create source rule 3 net-D


create rule security_rule
create action rule 4 accept
create comment rule 4 "comments"
create track rule 4 Log
create source rule 4 host_A
create destination rule 4 net-B
create service rule 4 http
create source rule 4 host_B
create destination rule 4 net-C
create service rule 4 service_X
create source rule 4 host_C
create destination rule 4 net-D
create service rule 4 service_y
create source rule 4 host_D
create source rule 4 host_E

这篇关于复杂的awk遍历CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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