首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Awk/Find/Grep -如果文件中存在$3

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

Stack Overflow用户
提问于 2013-09-24 13:41:24
回答 1查看 1.4K关注 0票数 3

为了简单而编辑。

添加现有变量:

代码语言:javascript
复制
time=`date +'%d%m%y_%H%M'`
temp_file=temp\_$input_file.txt
final=$time\_Parsed_CSV.config

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

代码语言:javascript
复制
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)的对象时,命令需要如下:

代码语言:javascript
复制
awk -F "[,|]" '{if ($2=="group") print "set security address-book global address-set",$1,"address-set",$3}' $temp_file >> $final

否则-假设它是一个正常的主机,并打印如下:

代码语言:javascript
复制
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):

代码语言:javascript
复制
set security address-book global address-set group_b address-set group_a

用于组中的正常主机(host_a在group_a中)

代码语言:javascript
复制
set security address-book global address-set group_a address host_a
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-24 14:14:18

我想你要找的是这样的东西:

代码语言:javascript
复制
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”是从哪里来的--只是猜测而已。如果你在你的问题中提供更多的具体信息,我们可能会帮助你更多。

根据你最新的问题,我认为这是你所需要的:

代码语言:javascript
复制
$ 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变量来避免分词或文件名的扩展,否则总有一天你会得到一个大惊喜。而不是这样:

代码语言:javascript
复制
time=`date +'%d%m%y_%H%M'`
temp_file=temp\_$input_file.txt
final=$time\_Parsed_CSV.config

这样做:

代码语言:javascript
复制
time=$(date +'%d%m%y_%H%M')
temp_file="temp_${input_file}.txt"
final="${time}_Parsed_CSV.config"

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

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

代码语言:javascript
复制
$ 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.
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18983298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档