3

Editing for simplicity.

Adding existing variables:

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

Original CSV ($temp_file) - actual names are not 'host/group' anything - need to filter based on $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

Need an AWK String to parse $2 'group' objects as follows:

When $2 = 'group' AND $3 = an object who is also defined as a group elsewhere (such as group_a), command needs to be:

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

Else - Assume it is a normal host, and print this:

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

I would expect the output to look like: For Nested Groups (group_A within group_b):

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

For normal hosts in a group (host_a in group_a)

set security address-book global address-set group_a address host_a
Numpty
  • 1,461
  • 5
  • 19
  • 28
  • Hang on - is "$temp_file" actually the same file as the CSV file you posted at the top of your question? Please edit your question to strip it down to just REPRESENTATIVE input file(s), the output you want from a script and the reasons why that would be the output as I think you're currently giving us far too much of what you think the solution would be and not nearly enough of what the fundamental problem is. – Ed Morton Sep 24 '13 at 16:14
  • I've updated it for clarity - let me know if that makes more sense – Numpty Sep 25 '13 at 12:57
  • It's better but I wish you'd just show the full expected output given that input file. – Ed Morton Sep 25 '13 at 13:00
  • Aside - why are you puting backslashes before the first underscores when setting your shell variables temp_file and final? Also - quote your shell variables. – Ed Morton Sep 25 '13 at 13:02

1 Answers1

3

I THINK what you're looking for is something like this:

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"

but it's hard to say without sample "$group_holder" contents and expected output. Hopefully that'll be enough for you to figure out any discrepancies though.

Looking at it again, I really don't think you need that "$group_holder" file but you don't tell us where "$temp_file" came from so again - just guessing. If you provide more concrete information in your question we can probably help you more.

Based on your updated question, I now think this is what you need:

$ 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

and you've GOT to quote your shell variables to avoid word splitting or file name expansion or you're going to get a big surprise some day. Instead of this:

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

Do this:

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

Always quote shell variables unless you have a very good, explicit reason not to and understand the consequences fully.

Commented version of script per OP request:

$ 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.
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thank you Ed - I will test that out in a moment. I've updated the original post to include what $group_holder looks like. $temp_file is simply a direct copy of the original CSV. – Numpty Sep 24 '13 at 15:09
  • I get an error trying to use that "awk: line 2: function address never defined – Numpty Sep 24 '13 at 15:14
  • Maybe just take out the `address` before the double open paren. I can't imagine that's what you actually want but it's a reasonable answer to the question as stated. – tripleee Sep 24 '13 at 15:39
  • Yeah, @triplee spotted the syntax error. I've fixed it now. As I mentioned I don't think you need $group_holder at all so please post a sample of $temp_file and some information on where that ORIGINALLY came from so we don't spend too much moire time tracking back to your actual source file(s). – Ed Morton Sep 24 '13 at 15:58
  • Ed - the CSV I posted looks exactly like $temp_file, minus the header. I'll play around with what you've provided and get back to you shortly. – Numpty Sep 24 '13 at 16:35
  • OK but to be clear - from what I can tell you are going about this the wrong way and you do not need $temp_file. – Ed Morton Sep 24 '13 at 16:44
  • Ed - I do need $temp_file (I'm making modifications directly to the file in other strings). I probably do not need $group_holder though...I just assumed it would be easier "with" it. Your string doesn't really do what I need though, I'll try and be more clear in the OP – Numpty Sep 25 '13 at 11:35
  • That works perfectly, thank you Ed. Would you mind explaining how that string works? Specifically the in gh ? "-set" : "") piece? (Trying to learn!) Thank you again – Numpty Sep 25 '13 at 14:03
  • 2
    I added a commented version of the script. – Ed Morton Sep 25 '13 at 14:14