为了简单而编辑。
添加现有变量:
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‘和$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我希望输出类似于: For嵌套组(group_A I group_b):
set security address-book global address-set group_b address-set group_a用于组中的正常主机(host_a在group_a中)
set security address-book global address-set group_a address host_a发布于 2013-09-24 14:14:18
我想你要找的是这样的东西:
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.https://stackoverflow.com/questions/18983298
复制相似问题