3

为简单起见进行编辑。

添加现有变量:

time=`date +'%d%m%y_%H%M'`
temp_file=temp\_$input_file.txt
final=$time\_Parsed_CSV.config

原始 CSV ($temp_file) - 实际名称不是“主机/组”任何东西 - 需要根据 $2 进行过滤

host_a,host,192.168.0.1
host_b,host,192.168.0.2
host_c,host,192.168.0.3
group_a,group,host_a
group_a,group,host_b
group_b,group,group_a
group_b,group,host_c

需要一个 AWK 字符串来解析 $2 'group' 对象,如下所示:

当 $2 = 'group' AND $3 = 在其他地方也被定义为组的对象(例如 group_a)时,命令需要是:

awk -F "[,|]" '{if ($2=="group") print "set security address-book global address-set",$1,"address-set",$3}' $temp_file >> $final

否则 - 假设它是一个普通主机,并打印:

awk -F "[,|]" '{if ($2=="group") print "set security address-book global address-set",$1,"address",$3}' $temp_file >> $final

我希望输出看起来像: 对于嵌套组(group_b 中的 group_A):

set security address-book global address-set group_b address-set group_a

对于组中的普通主机(group_a 中的 host_a)

set security address-book global address-set group_a address host_a
4

1 回答 1

3

我认为你正在寻找的是这样的:

awk -F'[,|]' 'NR==FNR{gh[$0];next} {print "set security address-book global", (($2=="group") && ($3 in gh) ? "address-set" : "address")}' "$group_holder" "your.csv"

但是如果没有示例“$group_holder”内容和预期输出就很难说。希望这足以让您找出任何差异。

再看一遍,我真的不认为你需要那个“$group_holder”文件,但你又没有告诉我们“$temp_file”是从哪里来的——只是猜测。如果您在问题中提供更具体的信息,我们可能会为您提供更多帮助。

根据您更新的问题,我现在认为这是您需要的:

$ awk -F',' '$2=="group" {if (NR==FNR) gh[$1]; else print "set security address-book global address-set", $1, "address" ($3 in gh ? "-set" : ""), $3}' "$temp_file" "$temp_file"
set security address-book global address-set group_a address host_a
set security address-book global address-set group_a address host_b
set security address-book global address-set group_b address-set group_a
set security address-book global address-set group_b address host_c

并且您必须引用您的 shell 变量以避免分词或文件名扩展,否则有一天您会大吃一惊。而不是这个:

time=`date +'%d%m%y_%H%M'`
temp_file=temp\_$input_file.txt
final=$time\_Parsed_CSV.config

做这个:

time=$(date +'%d%m%y_%H%M')
temp_file="temp_${input_file}.txt"
final="${time}_Parsed_CSV.config"

始终引用 shell 变量,除非您有一个非常好的、明确的理由不这样做并完全理解后果。

每个 OP 请求的脚本注释版本:

$ awk -F','      # Use comma as field separator
'
$2=="group" {     # Only do the following if $2 is "group"
   if (NR==FNR)   # IF this is the first pass of reading the input file THEN
      gh[$1];     # save the value of the first field as an index in the array "gh" (for "Group Holder")
   else           # ELSE this is the second pass of reading the input file so:
      print "set security address-book global address-set", $1, "address" 
      ($3 in gh ? "-set" : "") # Ternary operation (google it):
                               # if 3rd field exists as an index of gh then it
                               # was identified as a group during the first pass
                               # of reading the input file so add "-set" to the
                               # already printed "address" so it becomes
                               # "address-set", otherwise leave it as "address".
      , $3
}                 # end of if $2 is "group"
' "$temp_file" "$temp_file"    # read the input file twice.
于 2013-09-24T14:14:18.300 回答