使用bash或python将路由器配置文件转换为csv [英] Convert router configuration file to csv with bash or python
问题描述
我有一个配置文件,其中包含重复的行组(但组中行数不同),我希望将这些行合并到单个csv行中,以便更轻松地导入数据库. (数据库结构是灵活的).
I have a configuration file with repeating groups of lines (but different number of lines in a group) that I want to combine to single csv lines for easier importing to a database. (Database structure is flexible).
# Example:
lag 1
description "LAG-1 GOES TO LAG-2"
port 1/2/1
port 1/2/2
port 3/2/3
lacp active administrative-key 32770
exit
lag 10
description "REMOVED-LAG-10-0.0.0.0"
port 4/1/1
port 5/1/1
lacp active administrative-key 32771
exit
lag 11
description "REMOVED-LAG-11-4.4.4.4"
port 5/1/2
lacp active administrative-key 32772
exit
对于滞后"和退出"之间的每个块,我最终都需要以逗号分隔的行结束,就像这样:
I ultimately need to end up with comma-separated lines for each block between "lag" and "exit", like this:
1,"LAG 1 GOES TO LAG-2",32770,1/2/3
1,"LAG 1 GOES TO LAG-2",32770,1/2/2
1,"LAG 1 GOES TO LAG-2",32770,3/2/3
10,"REMOVED-LAG-10-0.0.0.0",32771,4/1/1
10,"REMOVED-LAG-10-0.0.0.0",32771,5/1/1
11,"REMOVED-LAG-11-4.4.4.4",32772,5/1/2
然后,我将导入到这样的表中:
Then I would import to a table like this:
lag-id | description | key | port
我尝试了在这里找到的各种awk单线,例如:
I've tried various awk one-liners that I found here, such as:
awk -v RS="lag" 'NR>1{$1=$1; print RS, $0}'
但是这似乎是垂直压缩线,所以我最终得到了
but that seems to compress the lines vertically, so i end up with
exit 3/2/3 "LAG 1 GOES TO LAG-2"
exit 4/2/3 "LAG 10 GOES TO LAG-3"
exit 4/1/1 "LAG 11 GOES TO LAG-21"
略微更新了配置,说明可能有两次LAG-x数字. 更新了添加lacp值以获取到每个csv行的管理密钥.
Updated config slightly, description may have LAG-x numbers twice. EDIT 2: Updated adding lacp values to get admin key to each csv line.
推荐答案
能否请您尝试以下操作.
Could you please try following.
awk -v OFS="," '
{
gsub(/\r/,"")
sub(/^ +/,"")
}
/lag/{
ind=$2
next
}
/description/ && match($0,/\".*\"/){
des=substr($0,RSTART,RLENGTH)
next
}
/port/{
print ind,des,$2
}
' Input_file
如果您的字符串可能是大写字母或小写字母或混合字母,则可以在上面的代码中添加BEGIN
部分和IGNORECASE=1
,然后无论如何都应匹配字符串.
In case your strings may be either capital or small letter or of mix then you could add a BEGIN
section with IGNORECASE=1
in above code and it should match strings in any case then.
说明:以下仅用于解释目的,请参阅上面的完整代码.
Explaination: Following is only for explanation purposes, refer complete code above please.
-v OFS=","
将输出字段分隔符设置为所有行的逗号.
-v OFS=","
setting output field separator as comma for all lines.
gsub(/\r/,"")
根据OP的注释将\ r控制m个字符全局替换为NULL.
gsub(/\r/,"")
globally substituting \r control m characters with NULL as per OP's comment they were there.
sub(/^ +/,"")
在这里用NULL替换行的初始空间.
sub(/^ +/,"")
Substituting initial space of line with NULL here.
/lag/{Ind=$2;next}
搜索行中有字符串滞后的行,然后创建变量Ind,其值是$ 2,下一步将跳过此后的所有其他语句.
/lag/{Ind=$2;next}
searching line which had string lag in it and creating variable Ind whose value is $2 next will skip all further statements from here onwards.
/description...../
查找其中具有字符串描述的行,然后使用awk的match函数在同一行中从"
到"
查找正则表达式.将匹配的正则表达式值保存到名为desc的变量中,下一步将跳过此处的所有其他语句.
/description...../
looking for line which has string description in it and then using match function of awk to look for regex from "
till "
in same line. Saving this matched regex value in variable named desc, next will skip all further statements from here.
/port/...
寻找其中具有字符串端口的行,然后根据OP的要求在此处打印ind,des,$ 2的值.
/port/...
looking for line which has string port in it and then printing value of ind,des,$2 here as per OP's requirement.
这篇关于使用bash或python将路由器配置文件转换为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!