awk help

Therese Demers tdemers7 at gmail.com
Sat Apr 15 18:35:13 UTC 2017


Hi Ernie!

I'm not sure if this will help you, but here's an idea:

I created a file called "testforernie":

$ cat testforernie
Address: 1.11.244.65/32
                Hits 0  Bytes 0 Name    Ref 2
        Address: 1.163.113.139/32
                Hits 0  Bytes 0 Name    Ref 2
        Address: 1.163.139.214/32
                Hits 0  Bytes 0 Name    Ref 2
        Address: 1.173.51.202/32
                Hits 0  Bytes 0 Name    Ref 2
        Address: 1.175.164.129/32
                Hits 0  Bytes 0 Name    Ref 2
        Address: 99.197.14.34/32
                Hits 0  Bytes 0 Name    Ref 2


$ cat testforernie | awk '{print}' ORS='' | sed
's%Address:%JellybeansAddress%g' | sed 's/Jellybeans/\'$'\n/g' | sed '/^$/d'
Address 1.11.244.65/32                Hits 0  Bytes 0 Name    Ref 2
Address 1.163.113.139/32                Hits 0  Bytes 0 Name    Ref 2
Address 1.163.139.214/32                Hits 0  Bytes 0 Name    Ref 2
Address 1.173.51.202/32                Hits 0  Bytes 0 Name    Ref 2
Address 1.175.164.129/32                Hits 0  Bytes 0 Name    Ref 2
Address 99.197.14.34/32                Hits 0  Bytes 0 Name    Ref 2

So if you wanted to get a pair containing the address and number of hits
you could do something like:
$ cat testforernie | awk '{print}' ORS='' | sed
's%Address:%JellybeansAddress%g' | sed 's/Jellybeans/\'$'\n/g' | sed
'/^$/d' | awk '{print $2,$4}'
1.11.244.65/32 0
1.163.113.139/32 0
1.163.139.214/32 0
1.173.51.202/32 0
1.175.164.129/32 0
99.197.14.34/32 0


Here's an explanation:

The "awk '{print}' ORS=''  part turns all the output into one line.

The "sed 's%Address:%JellybeansAddress%g'" part just makes a delimeter that
you can use to create to create a new line with (Jellybeans).

The "sed 's/Jellybeans/\'$'\n/g'" part substitutes the pattern "Jellybeans"
with a new line.

And the  "sed '/^$/d'" part gets rid of that empty line (The first instance
of Jellybeans that was substituted for a new line).



Hope this helps you! :)

-Therese


On Sat, Apr 15, 2017 at 10:59 AM, Jim Trigg <jtrigg at huiekin.org> wrote:

> /^Address/ { address = $2; got_address = 1; }
> /^ Hits/       { if (got_address) {
>                           hits = $2;
>                           if (hits) {
>                             [Do what's needed with address and hits]
>                             got_address = 0;
>                           } else {
>                              [Handle error condition of hits without
> address]
>                           }
>                       }
>
> Jim
>
> On April 15, 2017 1:36:01 PM EDT, Ernie Luzar <luzar722 at gmail.com> wrote:
> >Hello list;
> >
> >Can not figure how to process 2 lines as a single line.
> >I have this file.
> >
> >poollist: opts = 0x2
> >Name: test Role: ipf   References: 2   Hits: 0
> >       Nodes Starting at 0xc9fd4800
> >       Address: 1.11.244.65/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >       Address: 1.163.113.139/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >       Address: 1.163.139.214/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >       Address: 1.173.51.202/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >       Address: 1.175.164.129/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >       Address: 99.197.14.34/32
> >               Hits 0  Bytes 0 Name    Ref 2
> >
> >The Address line and the Hits line makes a pair.
> >
> >For any pair that hits is greater than zero
> >output file with ip address with ; sufix instead of /32 and
> >hit value in fixed position.
> >
> >I can't figure out how to code awk to process the 2 lines as a pair.
> >
> >All ready searched the online awk references and could not find an
> >example. Any pointers would sure help.
> >
> >Thanks
> >_______________________________________________
> >freebsd-questions at freebsd.org mailing list
> >https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> >To unsubscribe, send any mail to
> >"freebsd-questions-unsubscribe at freebsd.org"
>
> --
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-
> unsubscribe at freebsd.org"
>


More information about the freebsd-questions mailing list