Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Apr 2017 13:59:30 -0400
From:      Jim Trigg <jtrigg@huiekin.org>
To:        freebsd-questions@freebsd.org
Subject:   Re: awk help
Message-ID:  <7951DF71-5CD3-4B53-9CB4-13CAA8945983@huiekin.org>
In-Reply-To: <58F25A01.1060208@gmail.com>
References:  <58F25A01.1060208@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
/^Address/ { address =3D $2; got_address =3D 1; }
/^ Hits/       { if (got_address) {
                          hits =3D $2;
                          if (hits) {
                            [Do what's needed with address and hits]
                            got_address =3D 0;
                          } else {
                             [Handle error condition of hits without addre=
ss]
                          }
                      }

Jim

On April 15, 2017 1:36:01 PM EDT, Ernie Luzar <luzar722@gmail=2Ecom> wrote=
:
>Hello list;
>
>Can not figure how to process 2 lines as a single line=2E
>I have this file=2E
>
>poollist: opts =3D 0x2
>Name: test Role: ipf 	References: 2	Hits: 0
>	Nodes Starting at 0xc9fd4800
>	Address: 1=2E11=2E244=2E65/32
>		Hits 0	Bytes 0	Name 	Ref 2
>	Address: 1=2E163=2E113=2E139/32
>		Hits 0	Bytes 0	Name 	Ref 2
>	Address: 1=2E163=2E139=2E214/32
>		Hits 0	Bytes 0	Name 	Ref 2
>	Address: 1=2E173=2E51=2E202/32
>		Hits 0	Bytes 0	Name 	Ref 2
>	Address: 1=2E175=2E164=2E129/32
>		Hits 0	Bytes 0	Name 	Ref 2
>	Address: 99=2E197=2E14=2E34/32
>		Hits 0	Bytes 0	Name 	Ref 2
>
>The Address line and the Hits line makes a pair=2E
>
>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=2E
>
>I can't figure out how to code awk to process the 2 lines as a pair=2E
>
>All ready searched the online awk references and could not find an=20
>example=2E Any pointers would sure help=2E
>
>Thanks
>_______________________________________________
>freebsd-questions@freebsd=2Eorg mailing list
>https://lists=2Efreebsd=2Eorg/mailman/listinfo/freebsd-questions
>To unsubscribe, send any mail to
>"freebsd-questions-unsubscribe@freebsd=2Eorg"

--=20
Sent from my Android device with K-9 Mail=2E Please excuse my brevity=2E
From owner-freebsd-questions@freebsd.org  Sat Apr 15 18:35:13 2017
Return-Path: <owner-freebsd-questions@freebsd.org>
Delivered-To: freebsd-questions@mailman.ysv.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org
 [IPv6:2001:1900:2254:206a::19:1])
 by mailman.ysv.freebsd.org (Postfix) with ESMTP id A6895D3F471
 for <freebsd-questions@mailman.ysv.freebsd.org>;
 Sat, 15 Apr 2017 18:35:13 +0000 (UTC)
 (envelope-from tdemers7@gmail.com)
Received: from mail-lf0-x236.google.com (mail-lf0-x236.google.com
 [IPv6:2a00:1450:4010:c07::236])
 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
 (Client CN "smtp.gmail.com",
 Issuer "Google Internet Authority G2" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 19D529E6
 for <freebsd-questions@freebsd.org>; Sat, 15 Apr 2017 18:35:13 +0000 (UTC)
 (envelope-from tdemers7@gmail.com)
Received: by mail-lf0-x236.google.com with SMTP id 88so5224755lfr.0
 for <freebsd-questions@freebsd.org>; Sat, 15 Apr 2017 11:35:13 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=mime-version:in-reply-to:references:from:date:message-id:subject:to
 :cc; bh=a5zM532uhPYGRa0XEuuNqSR+v/Vm8R4LWotM2dTU2HQ=;
 b=tCfGgIU1FZTdLqM2uVUBsr1U4Xes1/Va5GxQy/WZFWNfJcD/jeko07GYIvNJLdwe14
 MWq+41VmCAnzaeVJJEG4GL9mc8fFSQ/FP1hs+CrZeUnbPk2qTk9HwFE2gL+I+QQLt5e/
 4tUrl6lRPVhwnFEG4cV4sqcvnnKYTJpaLB8IVZBFhNCKa+c0KdVcRNGX2KyCeR8iRNp6
 NzOvnzc0ACCemzDfVyw/5Ln9KITSia/qmnWpt6dbGGNZHmFLviEjsihjJmryp3R9WwBY
 pi+PMz9BKJvRc4lzF23eVmQuFuqzRDInr6sLchAwe1yonKHCMetjsUPNpztHZUJLiYN4
 LnDQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:mime-version:in-reply-to:references:from:date
 :message-id:subject:to:cc;
 bh=a5zM532uhPYGRa0XEuuNqSR+v/Vm8R4LWotM2dTU2HQ=;
 b=sHQOG1JQarNmORqyKbg/n+v2UdtXGDZw6/m/5WaoYj/QJ4LlfmrVfS2iGDs7Rb7jYA
 21TQXkdVYdFSQJWmHe1PhZTvyHdzH5+fXeippgqhD63VTJORbxKJR4Gi2sokxwoEwZgH
 t/AMEX98zJYX0a/lFl9TyfmnMZGdszGdScnMQPxWzhNXoM2iJwGxEZ75xa4nsFqqt0JG
 xNESUEnbAC9tz5ay2qKrVkC50cLp6guIBfoGoKNydbYw2cFZbrb3/ewvpiT8fk5Ja88T
 H7vcudNAKD+Dz5+ABSNtlz8D9KOudDHMH5SpjK5LLbEvUbE4vfKDXsfhV75Lt2Zq8pJf
 mZTg==
X-Gm-Message-State: AN3rC/5dF13EQ61bBGlllylxc8sDT9fHo+/Udpx14JbIi/WjxjvnO5uh
 opp0xOnJRJWuiCcoC3dbkofcjmskxxXw
X-Received: by 10.25.218.10 with SMTP id r10mr1025921lfg.123.1492281310094;
 Sat, 15 Apr 2017 11:35:10 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.25.205.4 with HTTP; Sat, 15 Apr 2017 11:35:09 -0700 (PDT)
In-Reply-To: <7951DF71-5CD3-4B53-9CB4-13CAA8945983@huiekin.org>
References: <58F25A01.1060208@gmail.com>
 <7951DF71-5CD3-4B53-9CB4-13CAA8945983@huiekin.org>
From: Therese Demers <tdemers7@gmail.com>
Date: Sat, 15 Apr 2017 11:35:09 -0700
Message-ID: <CAD7cR7gj9SpcXdwTaOvcKj-AYLSotWCpNKcCt-gvNYYoD3dYrg@mail.gmail.com>
Subject: Re: awk help
To: Jim Trigg <jtrigg@huiekin.org>
Cc: freebsd-questions@freebsd.org
Content-Type: text/plain; charset=UTF-8
X-Content-Filtered-By: Mailman/MimeDel 2.1.23
X-BeenThere: freebsd-questions@freebsd.org
X-Mailman-Version: 2.1.23
Precedence: list
List-Id: User questions <freebsd-questions.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-questions/>;
List-Post: <mailto:freebsd-questions@freebsd.org>
List-Help: <mailto:freebsd-questions-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Sat, 15 Apr 2017 18:35:13 -0000

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@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@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@freebsd.org mailing list
> >https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> >To unsubscribe, send any mail to
> >"freebsd-questions-unsubscribe@freebsd.org"
>
> --
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-
> unsubscribe@freebsd.org"
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?7951DF71-5CD3-4B53-9CB4-13CAA8945983>