Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The steady state of disks is full. -- Ken Thompson


devel / comp.lang.awk / repeating the last record after meeting a condtion

SubjectAuthor
* repeating the last record after meeting a condtionraj
+- Re: repeating the last record after meeting a condtionEd Morton
+* Re: repeating the last record after meeting a condtionJanis Papanagnou
|`- Re: repeating the last record after meeting a condtionraj
`- Re: repeating the last record after meeting a condtionKenny McCormack

1
repeating the last record after meeting a condtion

<29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=821&group=comp.lang.awk#821

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ac8:7b52:: with SMTP id m18mr21385308qtu.131.1626695718750; Mon, 19 Jul 2021 04:55:18 -0700 (PDT)
X-Received: by 2002:a25:8884:: with SMTP id d4mr32211259ybl.410.1626695718482; Mon, 19 Jul 2021 04:55:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Mon, 19 Jul 2021 04:55:18 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=49.206.53.127; posting-account=QvOHZQoAAACPAWJegNxpRnFc_Xh1y_DR
NNTP-Posting-Host: 49.206.53.127
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
Subject: repeating the last record after meeting a condtion
From: visitnag@gmail.com (raj)
Injection-Date: Mon, 19 Jul 2021 11:55:18 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: raj - Mon, 19 Jul 2021 11:55 UTC

Hi,

I have this sample file:

660312,MIKE , 138555.51
660312,MIKE , 219132.05
660312,MIKE , 246677.63
660312,MIKE , 268489.41 >>>>this record to be repeated
670182,JOHN , 155591.30
670182,JOHN , 246753.39
670182,JOHN , 279279.87
670182,JOHN , 303745.03
670182,JOHN , 408252.03 >>>>this record to be repeated

I am trying to repeat the last record where $1 is not matching with the next record.

awk -F, '{if(p!=$1){print $0; p=$1}print $0}'
But this is repeating the very first record after the condition is met.

Help will be appreciated.

Re: repeating the last record after meeting a condtion

<sd3rfh$gt7$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=822&group=comp.lang.awk#822

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortonspam@gmail.com (Ed Morton)
Newsgroups: comp.lang.awk
Subject: Re: repeating the last record after meeting a condtion
Date: Mon, 19 Jul 2021 07:33:47 -0500
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <sd3rfh$gt7$1@dont-email.me>
References: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 19 Jul 2021 12:33:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="16f109b75f660efd8c9812d1e9eb8838";
logging-data="17319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18A50V27q0eNGGqA4S4zXNX"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:Djh1cvu7vJiOuYvmdffpWQ7ehxQ=
In-Reply-To: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210718-8, 7/18/2021), Outbound message
 by: Ed Morton - Mon, 19 Jul 2021 12:33 UTC

On 7/19/2021 6:55 AM, raj wrote:
> Hi,
>
> I have this sample file:
>
> 660312,MIKE , 138555.51
> 660312,MIKE , 219132.05
> 660312,MIKE , 246677.63
> 660312,MIKE , 268489.41 >>>>this record to be repeated
> 670182,JOHN , 155591.30
> 670182,JOHN , 246753.39
> 670182,JOHN , 279279.87
> 670182,JOHN , 303745.03
> 670182,JOHN , 408252.03 >>>>this record to be repeated
>
> I am trying to repeat the last record where $1 is not matching with the next record.
>
> awk -F, '{if(p!=$1){print $0; p=$1}print $0}'
> But this is repeating the very first record after the condition is met.
>
> Help will be appreciated.

$ awk -F',' '
p1 != $1 { if (NR>1) print p0 }
{ print; p1=$1; p0=$0 }
END { print p0 }
' file
660312,MIKE , 138555.51
660312,MIKE , 219132.05
660312,MIKE , 246677.63
660312,MIKE , 268489.41 >>>>this record to be repeated
660312,MIKE , 268489.41 >>>>this record to be repeated
670182,JOHN , 155591.30
670182,JOHN , 246753.39
670182,JOHN , 279279.87
670182,JOHN , 303745.03
670182,JOHN , 408252.03 >>>>this record to be repeated
670182,JOHN , 408252.03 >>>>this record to be repeated

Regards,

Ed.

Re: repeating the last record after meeting a condtion

<sd3rlb$aru$1@news-1.m-online.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=823&group=comp.lang.awk#823

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!news.karotte.org!news.space.net!news.m-online.net!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: repeating the last record after meeting a condtion
Date: Mon, 19 Jul 2021 14:36:59 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 29
Message-ID: <sd3rlb$aru$1@news-1.m-online.net>
References: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
NNTP-Posting-Host: 2001:a61:241e:cc01:60f5:311:df9f:2d91
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: news-1.m-online.net 1626698219 11134 2001:a61:241e:cc01:60f5:311:df9f:2d91 (19 Jul 2021 12:36:59 GMT)
X-Complaints-To: news@news-1.m-online.net
NNTP-Posting-Date: Mon, 19 Jul 2021 12:36:59 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
In-Reply-To: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
 by: Janis Papanagnou - Mon, 19 Jul 2021 12:36 UTC

On 19.07.2021 13:55, raj wrote:
> Hi,
>
> I have this sample file:
>
> 660312,MIKE , 138555.51
> 660312,MIKE , 219132.05
> 660312,MIKE , 246677.63
> 660312,MIKE , 268489.41 >>>>this record to be repeated
> 670182,JOHN , 155591.30
> 670182,JOHN , 246753.39
> 670182,JOHN , 279279.87
> 670182,JOHN , 303745.03
> 670182,JOHN , 408252.03 >>>>this record to be repeated
>
> I am trying to repeat the last record where $1 is not matching with the next record.
>
> awk -F, '{if(p!=$1){print $0; p=$1}print $0}'
> But this is repeating the very first record after the condition is met.
>
> Help will be appreciated.

Maybe something like this...

awk -F, 'p && $1!=p {print d}; {p=$1; d=$0 }; 1; END{print d}'

Janis

Re: repeating the last record after meeting a condtion

<4a185b9e-6552-4ee9-8caa-7ec88f571e16n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=824&group=comp.lang.awk#824

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ad4:4bca:: with SMTP id l10mr24553760qvw.50.1626699695373; Mon, 19 Jul 2021 06:01:35 -0700 (PDT)
X-Received: by 2002:a25:8884:: with SMTP id d4mr32583247ybl.410.1626699694964; Mon, 19 Jul 2021 06:01:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Mon, 19 Jul 2021 06:01:34 -0700 (PDT)
In-Reply-To: <sd3rlb$aru$1@news-1.m-online.net>
Injection-Info: google-groups.googlegroups.com; posting-host=49.206.53.127; posting-account=QvOHZQoAAACPAWJegNxpRnFc_Xh1y_DR
NNTP-Posting-Host: 49.206.53.127
References: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com> <sd3rlb$aru$1@news-1.m-online.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4a185b9e-6552-4ee9-8caa-7ec88f571e16n@googlegroups.com>
Subject: Re: repeating the last record after meeting a condtion
From: visitnag@gmail.com (raj)
Injection-Date: Mon, 19 Jul 2021 13:01:35 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 30
 by: raj - Mon, 19 Jul 2021 13:01 UTC

On Monday, 19 July 2021 at 18:07:01 UTC+5:30, Janis Papanagnou wrote:
> On 19.07.2021 13:55, raj wrote:
> > Hi,
> >
> > I have this sample file:
> >
> > 660312,MIKE , 138555.51
> > 660312,MIKE , 219132.05
> > 660312,MIKE , 246677.63
> > 660312,MIKE , 268489.41 >>>>this record to be repeated
> > 670182,JOHN , 155591.30
> > 670182,JOHN , 246753.39
> > 670182,JOHN , 279279.87
> > 670182,JOHN , 303745.03
> > 670182,JOHN , 408252.03 >>>>this record to be repeated
> >
> > I am trying to repeat the last record where $1 is not matching with the next record.
> >
> > awk -F, '{if(p!=$1){print $0; p=$1}print $0}'
> > But this is repeating the very first record after the condition is met.
> >
> > Help will be appreciated.
> Maybe something like this...
>
> awk -F, 'p && $1!=p {print d}; {p=$1; d=$0 }; 1; END{print d}'
>
>
> Janis

Thank you very much

Re: repeating the last record after meeting a condtion

<sd3va7$3n5sf$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=825&group=comp.lang.awk#825

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: repeating the last record after meeting a condtion
Date: Mon, 19 Jul 2021 13:39:19 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sd3va7$3n5sf$1@news.xmission.com>
References: <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>
Injection-Date: Mon, 19 Jul 2021 13:39:19 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3905423"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 19 Jul 2021 13:39 UTC

In article <29cb9f58-7a9a-4e2b-a732-3d9a4661ccaen@googlegroups.com>,
raj <visitnag@gmail.com> wrote:
>Hi,
>
>I have this sample file:
>
>660312,MIKE , 138555.51
>660312,MIKE , 219132.05
>660312,MIKE , 246677.63
>660312,MIKE , 268489.41 >>>>this record to be repeated
>670182,JOHN , 155591.30
>670182,JOHN , 246753.39
>670182,JOHN , 279279.87
>670182,JOHN , 303745.03
>670182,JOHN , 408252.03 >>>>this record to be repeated
>
>I am trying to repeat the last record where $1 is not matching with the next record.
>
>awk -F, '{if(p!=$1){print $0; p=$1}print $0}'
>But this is repeating the very first record after the condition is met.

Others have given solutions using flag variables and such, that are
designed to work regardless of file size, but here's the way I'd look at
this problem. I'd assume that the file size isn't an issue - i.e., that
your file is probably not that big, and that the definition of a "big" file
on modern machines is orders of magnitude larger than what your grandfather
(and the original designers of AWK) would have considered to be a "big" file.

A pretty large fraction of the AWK programs that I write follow a general
pattern of:

1) In the main pattern/action space, build up an array "myArray".

2) In the END clause, dump out myArray.

This pattern fits your problem well. I'd do something like (untested):

--- Cut Here ---
BEGIN { FS="," }
{ myArray[$1] = $0 } # Keep only the last one seen.
END { setsort(2);for (i in myArray) print myArray[i] }
--- Cut Here ---

Now, the only issue is the order in which the final output comes. You may
not care what order the output is, in which case, you can leave out the
call to setsort(). Or, you can be aware that setsort(2) will make it come
out in ascending numeric order.

And, in fact, assuming you are using GAWK, it should come out in ascending
numeric order even without using setsort(). This seems to be a
barely-documented feature of GAWK - that if array indices are all numeric,
they will come out sorted numerically, even without explicitly setting the
sort order.

--
"You can safely assume that you have created God in your own image when
it turns out that God hates all the same people you do." -- Anne Lamott

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor