0

Is there a way to format the following output so that only lines 1 and the respective lines that state the "rule" name print out? Depending on the configuation, the "rule" line could vary. It could be the 4th, 5th, 8th line, etc. As shown, the rule name is in brackets. It would be best if the first line and the rule line come out together like show in the "desired output" example below.

command: show virtual server

Original output:

adc virtual name_of_virtual_server {
    output
    destination 10.201.111.101:80
    ip-protocol tcp
    mask 255.255.255.255
    pool test
    profiles {
        test {
            context clientside
        }
        test { }
        test data {
            context serverside
        }
    }
    rules {
        Exchange__all_services2.dat/Exchange__all_services2_rule7
    }
    source 0.0.0.0/0
    source-address-translation {
        pool data_pool
        type snat
    }
    vs-index 13
}

Desired output:

name_of_virtual_server
rules {
     Exchange__all_services2.dat/Exchange__all_services2_rule7

or - even better - with a blank line between entries:

Name: name_of_virtual_server
Rule: Exchange__all_services2.dat/Exchange__all_services2_rule7

Is this possible with sed or awk? Keep in mind there could be hundreds of virtual server configs in one config file if that matters.

2 Answers 2

0

Here is one way in perl:

perl -ne 'if (/^adc\s+\w+\s+(\w+)/) { print "Name: $1\n" }
          elsif (/^\s*rules\s*{/) { $inrule = 1; }
          elsif (/}/) { $inrule = 0; }
          elsif (/^\s*(\S*)/) { print "Rule: $1\n" if $inrule; }'

This goes through each line of the file, printing a Name: line when it sees an adc line, and then does a poor attempt at recognizing rules {} blocks, printing each line of those as Rule:.

It would be much more robust to use a proper parser for your input format; this one-liner will break if the layout isn't exactly as in your question.

4
  • To use the perl file, I assume I need to send the output to a file and then run the perl script against it, correct?
    – jogle900
    Mar 31, 2015 at 22:33
  • Would it be easier to sed or awk if the output was in one line like this. I found the option to do this. ex:
    – jogle900
    Mar 31, 2015 at 22:36
  • This perl one-liner is a pipe: it reads from stdin and writes to stdout, so you could do something like <file-in perl -ne 'if ...' >file-out, or use perl -i and pass a file name to do the replacement in-place.
    – dhag
    Mar 31, 2015 at 22:36
  • I found an option so that the output can be sent out as one big line. Example below. would that allow for an easier way using sed or awk? Ex: adc virtual Common/Exchange__all_services2.app/Exchange__all_services2_pop3 { app-Common/Exchange__all_services2.app/Exchange__all_services2 destination Common/10.201.111.101:pop3s ip-protocol tcp mask 255.255.255.255 pool test profiles { test_clientssl { context clientside } test profile { } more data { context serverside } } rules { test6790_irule7 } source 0.0.0.0/0 source-address-translation { pool test type snat } vs-index 13 }
    – jogle900
    Mar 31, 2015 at 22:42
0

If there is a proper parser for the file format of that file, you should use it. Lacking that, here is a solution that works for your sample input:

$ awk '/^adc/{print "name:",$3} /^[[:space:]]*rules/{getline;print "rule:",$1}' file
name: name_of_virtual_server
rule: Exchange__all_services2.dat/Exchange__all_services2_rule7

How it works

  • /^adc/{print "name:",$3}

    For any line that starts with adc, print out name: and the third item on the line.

  • /^[[:space:]]*rules/{getline;print "rule:",$1}

    For any line that starts with spaces (optional) followed by rules, this prints out the first item on the line which follows.

3
  • I'm not sure what you mean by proper parser? I don't have any options other than this. The script above prints out the name column just fine but no rules. The "rules" value may not always be the 3rd item. It will just be in the body of the output somewhere for each virtual server config and it varies. The output is just one huge line of values separate by spaces. This output has many virtual server config objects that include the name and rules with their respective values.
    – jogle900
    Apr 1, 2015 at 16:57
  • @jogle900 If that script's output is not in a standard format, then ignore my comment about a "proper parser."
    – John1024
    Apr 1, 2015 at 17:51
  • @jogle900 Some background: There are many questions here asking about using awk/sed to parse HTML. Although this can be done, sort of, it is much better to use a parser that understands HTML. There are also questions about using awk/sed to parse JSON. This also can be done, sort of, but it is much better to use a parser that understands JSON. Your script's output does not match any such standard parser that I am familiar with. But, if, hypothetically, there were such a parser, it would be better to use it.
    – John1024
    Apr 2, 2015 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .