Bash Awk/Find/Grep-如果文件中存在$3
为了简单而编辑 添加现有变量:Bash Awk/Find/Grep-如果文件中存在$3,bash,shell,awk,find,Bash,Shell,Awk,Find,为了简单而编辑 添加现有变量: time=`date +'%d%m%y_%H%M'` temp_file=temp\_$input_file.txt final=$time\_Parsed_CSV.config 原始CSV($temp_文件)-实际名称不是“主机/组”任何内容-需要基于$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_
time=`date +'%d%m%y_%H%M'`
temp_file=temp\_$input_file.txt
final=$time\_Parsed_CSV.config
原始CSV($temp_文件)-实际名称不是“主机/组”任何内容-需要基于$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
Else-假设它是普通主机,并打印以下内容:
awk -F "[,|]" '{if ($2=="group") print "set security address-book global address-set",$1,"address",$3}' $temp_file >> $final
我希望输出结果如下所示:
对于嵌套组(组b中的组A):
对于组中的普通主机(组中的主机a)
我想你要找的是这样的东西:
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_文件”是从哪里来的,只是猜测而已。如果您在问题中提供更多具体信息,我们可能会为您提供更多帮助
根据你最新的问题,我现在认为这是你需要的:
$ 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.
谢谢你,埃德,我马上就来测试一下。我更新了原来的帖子,加入了$group_holder的样子$temp_文件只是原始CSV的直接副本。我尝试使用它时出错“awk:第2行:从未定义的函数地址可能只是在打开双参数之前取出
地址
。”。我无法想象这是你真正想要的,但这是对问题的合理回答。是的,@triplee发现了语法错误。我现在已经修好了。正如我提到的,我认为您根本不需要$group_holder,所以请发布$temp_文件的示例以及一些关于该文件最初来源的信息,这样我们就不会花太多时间跟踪您的实际源文件。我添加了一个脚本的注释版本。请稍候-是“$temp_文件”实际上与您在问题顶部发布的CSV文件相同?请编辑您的问题,将其精简为具有代表性的输入文件,您希望从脚本中获得的输出以及为什么会是输出的原因,因为我认为您目前给我们的解决方案太多了,而根本问题的内容还远远不够。为了清晰起见,我对其进行了更新-让我知道这是否更有意义更好,但我希望您能展示完整的内容给定输入文件时的预期输出。搁置—为什么在设置shell变量temp_file和final时,在第一个下划线之前加反斜杠?另外-引用shell变量。
time=$(date +'%d%m%y_%H%M')
temp_file="temp_${input_file}.txt"
final="${time}_Parsed_CSV.config"
$ 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.