Awk / Find / Grep - 如果文件中存在$ 3

时间:2013-09-24 13:41:24

标签: bash shell awk find

为简单而编辑。

添加现有变量:

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'组'对象,如下所示:

当$ 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

我希望输出看起来像: 对于嵌套组(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

1 个答案:

答案 0 :(得分: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.