Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Nothing ever becomes real until it is experienced. -- John Keats


devel / comp.unix.shell / Re: Combine multiple awk calls into one.

SubjectAuthor
* Combine multiple awk calls into one.hongy...@gmail.com
`* Combine multiple awk calls into one.Kees Nuyt
 `* Combine multiple awk calls into one.hongy...@gmail.com
  `* Combine multiple awk calls into one.Janis Papanagnou
   `* Combine multiple awk calls into one.hongy...@gmail.com
    `* Combine multiple awk calls into one.Janis Papanagnou
     `* Combine multiple awk calls into one.Janis Papanagnou
      `* Combine multiple awk calls into one.hongy...@gmail.com
       `* Combine multiple awk calls into one.hongy...@gmail.com
        `* Combine multiple awk calls into one.Janis Papanagnou
         `* Combine multiple awk calls into one.Kenny McCormack
          +- Combine multiple awk calls into one.Janis Papanagnou
          +- Combine multiple awk calls into one.hongy...@gmail.com
          `* Combine multiple awk calls into one.Ben Bacarisse
           `* Combine multiple awk calls into one.hongy...@gmail.com
            +- Combine multiple awk calls into one.Janis Papanagnou
            `* Combine multiple awk calls into one.Ben Bacarisse
             `- Combine multiple awk calls into one.Kenny McCormack

1
Combine multiple awk calls into one.

<9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6498&group=comp.unix.shell#6498

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:bd44:0:b0:76c:ab4f:e9a7 with SMTP id n65-20020a37bd44000000b0076cab4fe9a7mr50584qkf.13.1690982400222;
Wed, 02 Aug 2023 06:20:00 -0700 (PDT)
X-Received: by 2002:a05:6808:16ab:b0:39c:f0c2:e3ad with SMTP id
bb43-20020a05680816ab00b0039cf0c2e3admr26380070oib.5.1690982399865; Wed, 02
Aug 2023 06:19:59 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 2 Aug 2023 06:19:59 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
Subject: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Wed, 02 Aug 2023 13:20:00 +0000
Content-Type: text/plain; charset="UTF-8"
 by: hongy...@gmail.com - Wed, 2 Aug 2023 13:19 UTC

Hi here,

I've written the following awk script:

--- begin here ---
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk 'BEGIN{i=0} /<dielectricfunction>/,\
/<\/dielectricfunction>/ \
{if ($1=="<r>") {a[i]=$2 ; b[i]=($3+$4+$5)/3 ; c[i]=$4 ; d[i]=$5 ; i=i+1}} \
END{for (j=0;j<i/2;j++) print a[j],b[j],b[j+i/2]}' vasprun.xml > optics-$1.dat

awk 'BEGIN{i=0} /<dielectricfunction comment="density-density">/,\
/<\/dielectricfunction>/ \
{if ($1=="<r>") {a[i]=$2 ; b[i]=($3+$4+$5)/3 ; c[i]=$4 ; d[i]=$5 ; i=i+1}} \
END{for (j=0;j<i/2;j++) print a[j],b[j],b[j+i/2]}' vasprun.xml > optics-density-$1.dat

awk 'BEGIN{i=0} /<dielectricfunction comment="current-current">/,\
/<\/dielectricfunction>/ \
{if ($1=="<r>") {a[i]=$2 ; b[i]=($3+$4+$5)/3 ; c[i]=$4 ; d[i]=$5 ; i=i+1}} \
END{for (j=0;j<i/2;j++) print a[j],b[j],b[j+i/2]}' vasprun.xml > optics-current-$1.dat
--- end here ---

But the above script have the following inconveniences:

1. It will generate empty out files.
2. It uses 3 awk calls.

I want to achieve the following goals by enhance the script:

1. If there is no matching data, no file is output.
2. Combine multiple awk calls into one.

Any tips/comments will be appreciated.

Regards,
Zhao

Re: Combine multiple awk calls into one.

<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6502&group=comp.unix.shell#6502

  copy link   Newsgroups: comp.unix.shell
From: k.nuyt@nospam.demon.nl (Kees Nuyt)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Wed, 02 Aug 2023 17:58:33 +0200
Reply-To: k.nuyt@nospam.demon.nl
Message-ID: <ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
User-Agent: ForteAgent/7.10.32.1214
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Organization: KPN B.V.
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!peer02.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 26
Injection-Date: Wed, 02 Aug 2023 17:58:33 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1427
 by: Kees Nuyt - Wed, 2 Aug 2023 15:58 UTC

On Wed, 2 Aug 2023 06:19:59 -0700 (PDT), "hongy...@gmail.com"
<hongyi.zhao@gmail.com> wrote:

>1. It will generate empty out files.
>2. It uses 3 awk calls.

1) Read the awk manual about redirection
2) Rewrite the bash script with one awk invocation
3) Pass the suffix as a variable to awk with
-v suffix=$suffix
4)
BEGIN{} and the body are the same as your current awk scripts
5) In one END{}, use the existing loop three times, with
redirections to each of the files. One example:
END{
....
....
for (j=0;j<i/2;j++){
print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"
}
} --
HTH
Kees Nuyt

Re: Combine multiple awk calls into one.

<058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6507&group=comp.unix.shell#6507

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ae9:df01:0:b0:763:a36e:19bc with SMTP id t1-20020ae9df01000000b00763a36e19bcmr77029qkf.5.1691020745979;
Wed, 02 Aug 2023 16:59:05 -0700 (PDT)
X-Received: by 2002:a05:6808:1b24:b0:3a3:7087:bbfb with SMTP id
bx36-20020a0568081b2400b003a37087bbfbmr28418309oib.6.1691020745767; Wed, 02
Aug 2023 16:59:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 2 Aug 2023 16:59:05 -0700 (PDT)
In-Reply-To: <ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com> <ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Wed, 02 Aug 2023 23:59:05 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2273
 by: hongy...@gmail.com - Wed, 2 Aug 2023 23:59 UTC

On Wednesday, August 2, 2023 at 11:58:51 PM UTC+8, Kees Nuyt wrote:
> On Wed, 2 Aug 2023 06:19:59 -0700 (PDT), "hongy...@gmail.com"
> <hongy...@gmail.com> wrote:
>
> >1. It will generate empty out files.
> >2. It uses 3 awk calls.
> 1) Read the awk manual about redirection
> 2) Rewrite the bash script with one awk invocation
> 3) Pass the suffix as a variable to awk with
> -v suffix=$suffix
> 4)
> BEGIN{} and the body are the same as your current awk scripts
> 5) In one END{}, use the existing loop three times, with
> redirections to each of the files. One example:
> END{
> ...
> ...
> for (j=0;j<i/2;j++){
> print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"

But how can I know this is corresponding to the case of matched xml tag `<dielectricfunction comment="current-current">'?

More specifically, I want to use the corresponding filename part based on the xml tag programmatically here.

> }
> }
> --
> HTH
> Kees Nuyt

Re: Combine multiple awk calls into one.

<uafo8g$loao$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6510&group=comp.unix.shell#6510

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Thu, 3 Aug 2023 10:24:48 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <uafo8g$loao$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
<058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 3 Aug 2023 08:24:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d12e624ee50e97b8955158d7afe0eab3";
logging-data="713048"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DyFTIW1RumKd5BCpVJMsB"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:5TZZL+JcZr/7886oD+ZfYV2j1LY=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
 by: Janis Papanagnou - Thu, 3 Aug 2023 08:24 UTC

On 03.08.2023 01:59, hongy...@gmail.com wrote:
> On Wednesday, August 2, 2023 at 11:58:51 PM UTC+8, Kees Nuyt wrote:
>> On Wed, 2 Aug 2023 06:19:59 -0700 (PDT), "hongy...@gmail.com"
>> <hongy...@gmail.com> wrote:
>>
>>> 1. It will generate empty out files.
>>> 2. It uses 3 awk calls.
>> 1) Read the awk manual about redirection
>> 2) Rewrite the bash script with one awk invocation
>> 3) Pass the suffix as a variable to awk with
>> -v suffix=$suffix
>> 4)
>> BEGIN{} and the body are the same as your current awk scripts
>> 5) In one END{}, use the existing loop three times, with
>> redirections to each of the files. One example:
>> END{
>> ...
>> ...
>> for (j=0;j<i/2;j++){
>> print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"
>
> But how can I know this is corresponding to the case of matched xml tag `<dielectricfunction comment="current-current">'?

You can, for example, simply store the three data types in different
array sets; a1[], b1[], ..., a2[], b2[], ..., a3[], b3[], ...
(and then don't forget to use own index variables i1, i2, i3).

(Also note that the /<dielectricfunction>/ data contains also the other
two data sets; in case that it's not intentional.)

>
> More specifically, I want to use the corresponding filename part based on the xml tag programmatically here.

This has already been answered by Kees; pass the suffix to awk...

awk -v suffix="$1" '
...
print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"
...
'

Janis

>
>> }
>> }
>> --
>> HTH
>> Kees Nuyt

Re: Combine multiple awk calls into one.

<4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6511&group=comp.unix.shell#6511

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:4e56:0:b0:405:4376:fed5 with SMTP id e22-20020ac84e56000000b004054376fed5mr96003qtw.3.1691070200713;
Thu, 03 Aug 2023 06:43:20 -0700 (PDT)
X-Received: by 2002:a05:6808:181f:b0:3a4:8115:5e7 with SMTP id
bh31-20020a056808181f00b003a4811505e7mr30053127oib.10.1691070200476; Thu, 03
Aug 2023 06:43:20 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Thu, 3 Aug 2023 06:43:20 -0700 (PDT)
In-Reply-To: <uafo8g$loao$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl> <058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 03 Aug 2023 13:43:20 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4517
 by: hongy...@gmail.com - Thu, 3 Aug 2023 13:43 UTC

On Thursday, August 3, 2023 at 4:24:55 PM UTC+8, Janis Papanagnou wrote:
> On 03.08.2023 01:59, hongy...@gmail.com wrote:
> > On Wednesday, August 2, 2023 at 11:58:51 PM UTC+8, Kees Nuyt wrote:
> >> On Wed, 2 Aug 2023 06:19:59 -0700 (PDT), "hongy...@gmail.com"
> >> <hongy...@gmail.com> wrote:
> >>
> >>> 1. It will generate empty out files.
> >>> 2. It uses 3 awk calls.
> >> 1) Read the awk manual about redirection
> >> 2) Rewrite the bash script with one awk invocation
> >> 3) Pass the suffix as a variable to awk with
> >> -v suffix=$suffix
> >> 4)
> >> BEGIN{} and the body are the same as your current awk scripts
> >> 5) In one END{}, use the existing loop three times, with
> >> redirections to each of the files. One example:
> >> END{
> >> ...
> >> ...
> >> for (j=0;j<i/2;j++){
> >> print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"
> >
> > But how can I know this is corresponding to the case of matched xml tag `<dielectricfunction comment="current-current">'?
> You can, for example, simply store the three data types in different
> array sets; a1[], b1[], ..., a2[], b2[], ..., a3[], b3[], ...
> (and then don't forget to use own index variables i1, i2, i3).
>
> (Also note that the /<dielectricfunction>/ data contains also the other
> two data sets; in case that it's not intentional.)
> >
> > More specifically, I want to use the corresponding filename part based on the xml tag programmatically here.
> This has already been answered by Kees; pass the suffix to awk...
>
> awk -v suffix="$1" '
> ...
> print a[j],b[j],b[j+i/2]} >"optics-current-" suffix ".dat"
> ...
> '

Do you mean something as follows?

#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '
/<dielectricfunction>/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5
i++
}
} /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a1[i1] = $2
b1[i1] = ($3 + $4 + $5) / 3
c1[i1] = $4
d1[i1] = $5
i1++
}
} /<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a2[i2] = $2
b2[i2] = ($3 + $4 + $5) / 3
c2[i2] = $4
d2[i2] = $5
i2++
}
} END {
for (j = 0; j < i / 2; j++) {
print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
}
for (j = 0; j < i1 / 2; j++) {
print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
}
for (j = 0; j < i2 / 2; j++) {
print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
}
} ' vasprun.xml


> Janis

Regards,
Zhao
> >
> >> }
> >> }
> >> --
> >> HTH
> >> Kees Nuyt

Re: Combine multiple awk calls into one.

<uagi06$qko8$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6512&group=comp.unix.shell#6512

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Thu, 3 Aug 2023 17:44:04 +0200
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <uagi06$qko8$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
<058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me>
<4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 3 Aug 2023 15:44:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d12e624ee50e97b8955158d7afe0eab3";
logging-data="873224"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PAMVu4YC1O2dFq3dMl05v"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:cnktZf5Kb46US/1Hb1S5nzNDOJc=
In-Reply-To: <4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Thu, 3 Aug 2023 15:44 UTC

On 03.08.2023 15:43, hongy...@gmail.com wrote:
> Do you mean something as follows?
>
> #!/bin/bash
> [...]
>

At a quick first glance I'd say yes, something like that. Does it do
the job as you expect it? - If it does then I'd also consider to use
a function for the whole if-statement and pass the array as argument
(and of course you'd also need to handle the indexes differently).
Maybe something like...

function assign (a, b, c, d, i)
{ i++
if ($1 == "<r>") {
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5
}
return i
}

/<dielectricfunction>/, /<\/dielectricfunction>/ {
i = assign(a, b, c, d, i)
}

/<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
i1 = assign(a1, b1, c1, d1, i1)
}

....etc...

Note: Arrays are passed by reference but scalars not, so you must pass
the index value and return it.

Janis

Re: Combine multiple awk calls into one.

<uagj5o$qs2b$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6513&group=comp.unix.shell#6513

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Thu, 3 Aug 2023 18:04:07 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <uagj5o$qs2b$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
<058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me>
<4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
<uagi06$qko8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 3 Aug 2023 16:04:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d12e624ee50e97b8955158d7afe0eab3";
logging-data="880715"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18O6uEI3FJlR7bSeNrnUZoJ"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:KLkN8G4rU+0XA9/xCIJxY2pH2Ig=
In-Reply-To: <uagi06$qko8$1@dont-email.me>
 by: Janis Papanagnou - Thu, 3 Aug 2023 16:04 UTC

On 03.08.2023 17:44, Janis Papanagnou wrote:
> On 03.08.2023 15:43, hongy...@gmail.com wrote:
>> Do you mean something as follows?
>>
>> #!/bin/bash
>> [...]
>>
>
> At a quick first glance I'd say yes, something like that. Does it do
> the job as you expect it? - If it does then I'd also consider to use
> a function for the whole if-statement and pass the array as argument
> (and of course you'd also need to handle the indexes differently).
> Maybe something like...
>
> function assign (a, b, c, d, i)
> {

Argh! - I just noticed you start by 0, so remove that i++ here and...

> i++
> if ($1 == "<r>") {
> a[i] = $2
> b[i] = ($3 + $4 + $5) / 3
> c[i] = $4
> d[i] = $5
> }
> return i

....add the '++' here...

return ++i

> }
>
> /<dielectricfunction>/, /<\/dielectricfunction>/ {
> i = assign(a, b, c, d, i)
> }
>
> /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
> i1 = assign(a1, b1, c1, d1, i1)
> }
>
> ...etc...
>
> Note: Arrays are passed by reference but scalars not, so you must pass
> the index value and return it.
>
> Janis
>

Re: Combine multiple awk calls into one.

<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6515&group=comp.unix.shell#6515

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:1746:b0:403:b12b:881d with SMTP id l6-20020a05622a174600b00403b12b881dmr1754qtk.4.1691117610538;
Thu, 03 Aug 2023 19:53:30 -0700 (PDT)
X-Received: by 2002:a05:687c:354a:b0:1bb:9fd4:65ed with SMTP id
li10-20020a05687c354a00b001bb9fd465edmr462245oac.5.1691117610172; Thu, 03 Aug
2023 19:53:30 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Thu, 3 Aug 2023 19:53:29 -0700 (PDT)
In-Reply-To: <uagj5o$qs2b$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl> <058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me> <4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
<uagi06$qko8$1@dont-email.me> <uagj5o$qs2b$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Fri, 04 Aug 2023 02:53:30 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 7522
 by: hongy...@gmail.com - Fri, 4 Aug 2023 02:53 UTC

On Friday, August 4, 2023 at 12:04:14 AM UTC+8, Janis Papanagnou wrote:
> On 03.08.2023 17:44, Janis Papanagnou wrote:
> > On 03.08.2023 15:43, hongy...@gmail.com wrote:
> >> Do you mean something as follows?
> >>
> >> #!/bin/bash
> >> [...]
> >>
> >
> > At a quick first glance I'd say yes, something like that. Does it do
> > the job as you expect it? - If it does then I'd also consider to use
> > a function for the whole if-statement and pass the array as argument
> > (and of course you'd also need to handle the indexes differently).
> > Maybe something like...
> >
> > function assign (a, b, c, d, i)
> > {
> Argh! - I just noticed you start by 0, so remove that i++ here and...
> > i++
> > if ($1 == "<r>") {
> > a[i] = $2
> > b[i] = ($3 + $4 + $5) / 3
> > c[i] = $4
> > d[i] = $5
> > }
> > return i
> ...add the '++' here...
>
> return ++i
> > }
> >
> > /<dielectricfunction>/, /<\/dielectricfunction>/ {
> > i = assign(a, b, c, d, i)
> > }
> >
> > /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
> > i1 = assign(a1, b1, c1, d1, i1)
> > }
> >
> > ...etc...

I ran the following tests, but it seems that neither my nor your approach works perfectly:

$ cat test.xml
<dielectricfunction comment="density-density">
<r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
<r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
<r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
<r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
<r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
<r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
</dielectricfunction>

<dielectricfunction comment="current-current">
<r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
<r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
<r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
<r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
<r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
<r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
</dielectricfunction>

<dielectricfunction>
<r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
<r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
<r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
<r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
<r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
<r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
</dielectricfunction>

$ cat awk.1
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '
/<dielectricfunction>/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5
i++
} }
/<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a1[i1] = $2
b1[i1] = ($3 + $4 + $5) / 3
c1[i1] = $4
d1[i1] = $5
i1++
} }
/<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
a2[i2] = $2
b2[i2] = ($3 + $4 + $5) / 3
c2[i2] = $4
d2[i2] = $5
i2++
} }
END {
for (j = 0; j < i / 2; j++) {
print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
} for (j = 0; j < i1 / 2; j++) {
print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
} for (j = 0; j < i2 / 2; j++) {
print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
} }
' test.xml

$ cat awk.2
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '

function assign (a, b, c, d, i)
{ if ($1 == "<r>") {
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5
} return i
i++
}

/<dielectricfunction>/, /<\/dielectricfunction>/ {
i = assign(a, b, c, d, i)
}

/<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
i1 = assign(a1, b1, c1, d1, i1)
}

/<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
i1 = assign(a1, b1, c1, d1, i1)
}

END {
for (j = 0; j < i / 2; j++) {
print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
} for (j = 0; j < i1 / 2; j++) {
print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
} for (j = 0; j < i2 / 2; j++) {
print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
} }
' test.xml

The tests are as follows:

First test my original script:

$ bash awk.1 dft
$ cat optics-dft.dat
0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149
$ cat optics-current-dft.dat
0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149
$ cat optics-density-dft.dat
0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

As you can see, the first line in the result only has one column.

Then I will test the version based on your suggested improvements:

$ rm *dat
$ bash awk.2 dft
$ ls *dat
ls: cannot access '*dat': No such file or directory

As you can see, nothing is generated at all.

> >
> > Note: Arrays are passed by reference but scalars not, so you must pass
> > the index value and return it.
> >
> > Janis

Zhao

Re: Combine multiple awk calls into one.

<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6516&group=comp.unix.shell#6516

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:3f49:b0:76c:c13b:2e24 with SMTP id ty9-20020a05620a3f4900b0076cc13b2e24mr4510qkn.6.1691131189042;
Thu, 03 Aug 2023 23:39:49 -0700 (PDT)
X-Received: by 2002:a05:6870:8c31:b0:1bf:5778:d1a9 with SMTP id
ec49-20020a0568708c3100b001bf5778d1a9mr893894oab.1.1691131188692; Thu, 03 Aug
2023 23:39:48 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Thu, 3 Aug 2023 23:39:48 -0700 (PDT)
In-Reply-To: <b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl> <058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me> <4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
<uagi06$qko8$1@dont-email.me> <uagj5o$qs2b$1@dont-email.me> <b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Fri, 04 Aug 2023 06:39:49 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 364
 by: hongy...@gmail.com - Fri, 4 Aug 2023 06:39 UTC

On Friday, August 4, 2023 at 10:53:33 AM UTC+8, hongy...@gmail.com wrote:
> On Friday, August 4, 2023 at 12:04:14 AM UTC+8, Janis Papanagnou wrote:
> > On 03.08.2023 17:44, Janis Papanagnou wrote:
> > > On 03.08.2023 15:43, hongy...@gmail.com wrote:
> > >> Do you mean something as follows?
> > >>
> > >> #!/bin/bash
> > >> [...]
> > >>
> > >
> > > At a quick first glance I'd say yes, something like that. Does it do
> > > the job as you expect it? - If it does then I'd also consider to use
> > > a function for the whole if-statement and pass the array as argument
> > > (and of course you'd also need to handle the indexes differently).
> > > Maybe something like...
> > >
> > > function assign (a, b, c, d, i)
> > > {
> > Argh! - I just noticed you start by 0, so remove that i++ here and...
> > > i++
> > > if ($1 == "<r>") {
> > > a[i] = $2
> > > b[i] = ($3 + $4 + $5) / 3
> > > c[i] = $4
> > > d[i] = $5
> > > }
> > > return i
> > ...add the '++' here...
> >
> > return ++i
> > > }
> > >
> > > /<dielectricfunction>/, /<\/dielectricfunction>/ {
> > > i = assign(a, b, c, d, i)
> > > }
> > >
> > > /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
> > > i1 = assign(a1, b1, c1, d1, i1)
> > > }
> > >
> > > ...etc...
> I ran the following tests, but it seems that neither my nor your approach works perfectly:
>
> $ cat test.xml
> <dielectricfunction comment="density-density">
> <r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
> <r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
> <r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
> <r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
> <r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
> <r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
> </dielectricfunction>
>
> <dielectricfunction comment="current-current">
> <r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
> <r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
> <r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
> <r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
> <r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
> <r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
> </dielectricfunction>
>
> <dielectricfunction>
> <r> 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 </r>
> <r> 0.0200 0.0030 0.0030 0.0030 0.0000 0.0000 0.0000 </r>
> <r> 0.0400 0.0059 0.0059 0.0059 0.0000 0.0000 0.0000 </r>
> <r> 0.0601 0.0089 0.0089 0.0089 0.0000 0.0000 0.0000 </r>
> <r> 0.0801 0.0119 0.0119 0.0119 0.0000 0.0000 0.0000 </r>
> <r> 0.1001 0.0149 0.0149 0.0149 0.0000 0.0000 0.0000 </r>
> </dielectricfunction>
>
> $ cat awk.1
> #!/bin/bash
>
> if [ -z "$1" ]; then
> echo "Usage: $0 <suffix>"
> exit 1
> fi
>
> suffix=$1
>
> awk -v suffix="$suffix" '
> /<dielectricfunction>/, /<\/dielectricfunction>/ {
> if ($1 == "<r>") {
> a[i] = $2
> b[i] = ($3 + $4 + $5) / 3
> c[i] = $4
> d[i] = $5
> i++
> }
> }
> /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
> if ($1 == "<r>") {
> a1[i1] = $2
> b1[i1] = ($3 + $4 + $5) / 3
> c1[i1] = $4
> d1[i1] = $5
> i1++
> }
> }
> /<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
> if ($1 == "<r>") {
> a2[i2] = $2
> b2[i2] = ($3 + $4 + $5) / 3
> c2[i2] = $4
> d2[i2] = $5
> i2++
> }
> }
> END {
> for (j = 0; j < i / 2; j++) {
> print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
> }
> for (j = 0; j < i1 / 2; j++) {
> print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
> }
> for (j = 0; j < i2 / 2; j++) {
> print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
> }
> }
> ' test.xml
>
>
> $ cat awk.2
> #!/bin/bash
>
> if [ -z "$1" ]; then
> echo "Usage: $0 <suffix>"
> exit 1
> fi
>
> suffix=$1
>
> awk -v suffix="$suffix" '
> function assign (a, b, c, d, i)
> {
> if ($1 == "<r>") {
> a[i] = $2
> b[i] = ($3 + $4 + $5) / 3
> c[i] = $4
> d[i] = $5
> }
> return i
> i++
> }
>
> /<dielectricfunction>/, /<\/dielectricfunction>/ {
> i = assign(a, b, c, d, i)
> }
>
> /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
> i1 = assign(a1, b1, c1, d1, i1)
> }
> /<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
> i1 = assign(a1, b1, c1, d1, i1)
> }
> END {
> for (j = 0; j < i / 2; j++) {
> print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
> }
> for (j = 0; j < i1 / 2; j++) {
> print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
> }
> for (j = 0; j < i2 / 2; j++) {
> print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
> }
> }
> ' test.xml
>
> The tests are as follows:
>
> First test my original script:
>
> $ bash awk.1 dft
> $ cat optics-dft.dat
> 0.0089
> 0.0200 0.003 0.0119
> 0.0400 0.0059 0.0149
> $ cat optics-current-dft.dat
> 0.0089
> 0.0200 0.003 0.0119
> 0.0400 0.0059 0.0149
> $ cat optics-density-dft.dat
> 0.0089
> 0.0200 0.003 0.0119
> 0.0400 0.0059 0.0149
>
> As you can see, the first line in the result only has one column.
>
> Then I will test the version based on your suggested improvements:
>
> $ rm *dat
> $ bash awk.2 dft
> $ ls *dat
> ls: cannot access '*dat': No such file or directory
>
> As you can see, nothing is generated at all.

Change to the following does the trick:

$ cat awk.1
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '
/<dielectricfunction>/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
i++
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5
}
} /<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
i1++
a1[i1] = $2
b1[i1] = ($3 + $4 + $5) / 3
c1[i1] = $4
d1[i1] = $5
}
} /<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
if ($1 == "<r>") {
i2++
a2[i2] = $2
b2[i2] = ($3 + $4 + $5) / 3
c2[i2] = $4
d2[i2] = $5
}
} END {
for (j = 1; j <= i / 2; j++) {
print a[j], b[j], b[j + i / 2] > "optics-" suffix ".dat"
}
for (j = 1; j <= i1 / 2; j++) {
print a1[j], b1[j], b1[j + i1 / 2] > "optics-density-" suffix ".dat"
}
for (j = 1; j <= i2 / 2; j++) {
print a2[j], b2[j], b2[j + i2 / 2] > "optics-current-" suffix ".dat"
}
} ' test.xml

$ cat awk.2
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '
function assign (a, b, c, d, i) {
if ($1 == "<r>") {
i++
a[i] = $2
b[i] = ($3 + $4 + $5) / 3
c[i] = $4
d[i] = $5

}
return i
}

/<dielectricfunction>/, /<\/dielectricfunction>/ {
i = assign(a, b, c, d, i)
}


Click here to read the complete article
Re: Combine multiple awk calls into one.

<uailea$18aba$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6517&group=comp.unix.shell#6517

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Fri, 4 Aug 2023 12:55:05 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <uailea$18aba$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<ptukcidmt5ttfpc44ogp83klf7k025br83@dim53.demon.nl>
<058c9f98-f1e2-4492-87a6-a960f17e07f6n@googlegroups.com>
<uafo8g$loao$1@dont-email.me>
<4330a2e1-902e-44ae-a046-23a2cdff5991n@googlegroups.com>
<uagi06$qko8$1@dont-email.me> <uagj5o$qs2b$1@dont-email.me>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Aug 2023 10:55:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b6483f700969c62b4b44792b8c756da0";
logging-data="1321322"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19s4W/KfnpOKavDCGSUBm5D"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:xA1D1ptRScAu193ca6hBvqjO5bM=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
 by: Janis Papanagnou - Fri, 4 Aug 2023 10:55 UTC

On 04.08.2023 08:39, hongy...@gmail.com wrote:
> On Friday, August 4, 2023 at 10:53:33 AM UTC+8, hongy...@gmail.com wrote:
>> I ran the following tests, but it seems that neither my nor your approach works perfectly:

I merely tried to show you the direction. I haven't analyzed
the code, just transcribed it a bit.

>> return i
>> i++

(The increment will not be reached. I meant to use 'return ++i'.)

>
> Change to the following does the trick:
> [...]

Fine. So I suppose it now works for you.

> if ($1 == "<r>") {
> i++

Well spotted; the i++ must indeed be inside the 'if' block.

Janis

Re: Combine multiple awk calls into one.

<uaimjh$34hq8$1@news.xmission.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6518&group=comp.unix.shell#6518

  copy link   Newsgroups: comp.unix.shell
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.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Fri, 4 Aug 2023 11:14:57 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <uaimjh$34hq8$1@news.xmission.com>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com> <b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com> <39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com> <uailea$18aba$1@dont-email.me>
Injection-Date: Fri, 4 Aug 2023 11:14:57 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3295048"; 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 - Fri, 4 Aug 2023 11:14 UTC

In article <uailea$18aba$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>On 04.08.2023 08:39, hongy...@gmail.com wrote:
>> On Friday, August 4, 2023 at 10:53:33AM UTC+8, hongy...@gmail.com wrote:
>>> I ran the following tests, but it seems that neither my nor your approach
>works perfectly:
>
>I merely tried to show you the direction. I haven't analyzed
>the code, just transcribed it a bit.
>
>>> return i
>>> i++
>
>(The increment will not be reached. I meant to use 'return ++i'.)

I note that your general approach is that you have multiple arrays, running
multiple counters (i, i1, etc) - one counter for each array.

In situations like this, I often use the trick of storing the counter in
the zero element of each array. So, you end up with something like:

A[++A[0]] = "something"

Also, OP might consider using a single, multi-dimensional array, instead of
multiple arrays (a,b,c,d). I often find this works better. Of course, you
have to be running a version of AWK that supports real multi-dimensional
arrays (i.e., TAWK or GAWK).

I'd be willing to bet that OP either is or should be running GAWK.

--
I love the poorly educated.

Re: Combine multiple awk calls into one.

<uainu8$18mlg$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6519&group=comp.unix.shell#6519

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Fri, 4 Aug 2023 13:37:43 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <uainu8$18mlg$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Aug 2023 11:37:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b6483f700969c62b4b44792b8c756da0";
logging-data="1333936"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19m4ZKaKEQ4O2tmnVdMhImK"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:hlUJyq0SJ3bxDb3HiN1nbFVoJTM=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uaimjh$34hq8$1@news.xmission.com>
 by: Janis Papanagnou - Fri, 4 Aug 2023 11:37 UTC

On 04.08.2023 13:14, Kenny McCormack wrote:
>
> I note that your general approach is that you have multiple arrays, running
> multiple counters (i, i1, etc) - one counter for each array.

(The OP's approach. I just transcribed the arrays from the individual
awk instances into one awk instance.)

>
> In situations like this, I often use the trick of storing the counter in
> the zero element of each array. So, you end up with something like:
>
> A[++A[0]] = "something"
>
> Also, OP might consider using a single, multi-dimensional array, instead of
> multiple arrays (a,b,c,d). I often find this works better. Of course, you
> have to be running a version of AWK that supports real multi-dimensional
> arrays (i.e., TAWK or GAWK).

Both of your ideas I considered - the first one in a slightly different
way; yours is interesting because of the array-(index,data)-coupling -,
but I abstained to suggest them for reasons of (presumed) simplicity.

Good points anyway.

>
> I'd be willing to bet that OP either is or should be running GAWK.

I suppose he does.

Janis

Re: Combine multiple awk calls into one.

<c18937bc-5a6f-4a20-a7d9-c2b0256da5ebn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6520&group=comp.unix.shell#6520

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:709a:0:b0:40f:eaf1:1c01 with SMTP id y26-20020ac8709a000000b0040feaf11c01mr8590qto.1.1691154274855;
Fri, 04 Aug 2023 06:04:34 -0700 (PDT)
X-Received: by 2002:a05:6808:30a0:b0:3a1:f2a4:3d7 with SMTP id
bl32-20020a05680830a000b003a1f2a403d7mr2382866oib.1.1691154274625; Fri, 04
Aug 2023 06:04:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Fri, 4 Aug 2023 06:04:34 -0700 (PDT)
In-Reply-To: <uaimjh$34hq8$1@news.xmission.com>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com> <39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c18937bc-5a6f-4a20-a7d9-c2b0256da5ebn@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Fri, 04 Aug 2023 13:04:34 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 91
 by: hongy...@gmail.com - Fri, 4 Aug 2023 13:04 UTC

On Friday, August 4, 2023 at 7:15:03 PM UTC+8, Kenny McCormack wrote:
> In article <uailea$18aba$1...@dont-email.me>,
> Janis Papanagnou <janis_pap...@hotmail.com> wrote:
> >On 04.08.2023 08:39, hongy...@gmail.com wrote:
> >> On Friday, August 4, 2023 at 10:53:33AM UTC+8, hongy...@gmail.com wrote:
> >>> I ran the following tests, but it seems that neither my nor your approach
> >works perfectly:
> >
> >I merely tried to show you the direction. I haven't analyzed
> >the code, just transcribed it a bit.
> >
> >>> return i
> >>> i++
> >
> >(The increment will not be reached. I meant to use 'return ++i'.)
> I note that your general approach is that you have multiple arrays, running
> multiple counters (i, i1, etc) - one counter for each array.
>
> In situations like this, I often use the trick of storing the counter in
> the zero element of each array. So, you end up with something like:
>
> A[++A[0]] = "something"
>
> Also, OP might consider using a single, multi-dimensional array, instead of
> multiple arrays (a,b,c,d). I often find this works better. Of course, you
> have to be running a version of AWK that supports real multi-dimensional
> arrays (i.e., TAWK or GAWK).

Do you mean something like the following?

#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <suffix>"
exit 1
fi

suffix=$1

awk -v suffix="$suffix" '
function assign(arr, idx, val1, val2, val3) {
if ($1 == "<r>") {
idx = ++arr[0]
val1[idx] = $2
val2[idx] = ($3 + $4 + $5) / 3
val3[idx] = $5
}
return idx
}

function print_to_file(arr1, arr2, filename) {
for (j = 1; j <= arr1[0] / 2; j++) {
print arr1[j], arr2[j], arr2[j + arr1[0] / 2] > filename
}
}

/<dielectricfunction>/, /<\/dielectricfunction>/ {
i = assign(a, i, a, b, c, d)
}

/<dielectricfunction comment="density-density">/, /<\/dielectricfunction>/ {
i1 = assign(a1, i1, a1, b1, c1, d1)
}

/<dielectricfunction comment="current-current">/, /<\/dielectricfunction>/ {
i2 = assign(a2, i2, a2, b2, c2, d2)
}

END {
print_to_file(a, b, "optics-" suffix ".dat")
print_to_file(a1, b1, "optics-density-" suffix ".dat")
print_to_file(a2, b2, "optics-current-" suffix ".dat")
} ' test.xml

> I'd be willing to bet that OP either is or should be running GAWK.

Yes. GAWK.
Zhao
> --
> I love the poorly educated.

Re: Combine multiple awk calls into one.

<87v8duud6a.fsf@bsb.me.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6521&group=comp.unix.shell#6521

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Fri, 04 Aug 2023 14:53:17 +0100
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <87v8duud6a.fsf@bsb.me.uk>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="82be12c07f28e99381fe1dae5b0b0603";
logging-data="1375281"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Iz0vyUvfIHLQEAAStQwagNgyTHXBVhdc="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:zYAxCmdQEybvG4QFGyZfA3KE7C4=
sha1:+NPun4lUXwRy1yjn379UD8xVm1o=
X-BSB-Auth: 1.d18b695d69294e6c72f2.20230804145317BST.87v8duud6a.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 4 Aug 2023 13:53 UTC

gazelle@shell.xmission.com (Kenny McCormack) writes:

> I note that your general approach is that you have multiple arrays, running
> multiple counters (i, i1, etc) - one counter for each array.
>
> In situations like this, I often use the trick of storing the counter in
> the zero element of each array. So, you end up with something like:
>
> A[++A[0]] = "something"

This should not be necessary. Some languages allow

A[] = "something"

as a way to append to an array, but not AWK. One might think that

A[length(A)] = "something"

would do the trick, but calling length before using A as an array turns
A into a scalar so you get a run-time error when A is indexed.

You can, however, do this:

BEGIN { delete A }
...
A[length(A)] = "something"

which is rather oblique, but works on all the awks I have installed
(gawk, nawk, mawk and original-awk). (You can write 'delete A[0]' if
you don't want to use the newer POSIX syntax.)

--
Ben.

Re: Combine multiple awk calls into one.

<0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6531&group=comp.unix.shell#6531

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:620a:948c:b0:768:3e5f:a946 with SMTP id sn12-20020a05620a948c00b007683e5fa946mr10015qkn.13.1691211971969;
Fri, 04 Aug 2023 22:06:11 -0700 (PDT)
X-Received: by 2002:a05:6808:199c:b0:3a7:5f99:9fe1 with SMTP id
bj28-20020a056808199c00b003a75f999fe1mr5556100oib.2.1691211971676; Fri, 04
Aug 2023 22:06:11 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Fri, 4 Aug 2023 22:06:11 -0700 (PDT)
In-Reply-To: <87v8duud6a.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=208.86.32.27; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 208.86.32.27
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com> <39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com> <87v8duud6a.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com>
Subject: Re: Combine multiple awk calls into one.
From: hongyi.zhao@gmail.com (hongy...@gmail.com)
Injection-Date: Sat, 05 Aug 2023 05:06:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: hongy...@gmail.com - Sat, 5 Aug 2023 05:06 UTC

On Friday, August 4, 2023 at 9:53:25 PM UTC+8, Ben Bacarisse wrote:
> gaz...@shell.xmission.com (Kenny McCormack) writes:
>
> > I note that your general approach is that you have multiple arrays, running
> > multiple counters (i, i1, etc) - one counter for each array.
> >
> > In situations like this, I often use the trick of storing the counter in
> > the zero element of each array. So, you end up with something like:
> >
> > A[++A[0]] = "something"
> This should not be necessary. Some languages allow
>
> A[] = "something"
>
> as a way to append to an array, but not AWK. One might think that

Your first say "but not AWK" here.
> A[length(A)] = "something"
>
> would do the trick, but calling length before using A as an array turns
> A into a scalar so you get a run-time error when A is indexed.
>
> You can, however, do this:
>
> BEGIN { delete A }
> ...
> A[length(A)] = "something"
>
> which is rather oblique, but works on all the awks I have installed
> (gawk, nawk, mawk and original-awk). (You can write 'delete A[0]' if
> you don't want to use the newer POSIX syntax.)

Then you say your suggestion is for AWK here.

So, I'm confused on what do you really mean.
> --
> Ben.

Zhao

Re: Combine multiple awk calls into one.

<ual3it$1mj9u$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6535&group=comp.unix.shell#6535

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Sat, 5 Aug 2023 11:08:44 +0200
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <ual3it$1mj9u$1@dont-email.me>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com>
<87v8duud6a.fsf@bsb.me.uk>
<0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 5 Aug 2023 09:08:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a08dec15ae0df8914696edb368dd6267";
logging-data="1789246"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191DrxjKLicZk7j+m9AevBp"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:bXi5hRmj8/ziPMSLXuyyKA5TMyw=
In-Reply-To: <0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 5 Aug 2023 09:08 UTC

On 05.08.2023 07:06, hongy...@gmail.com wrote:
> On Friday, August 4, 2023 at 9:53:25 PM UTC+8, Ben Bacarisse wrote:
>> gaz...@shell.xmission.com (Kenny McCormack) writes:
>>
>>> I note that your general approach is that you have multiple arrays, running
>>> multiple counters (i, i1, etc) - one counter for each array.
>>>
>>> In situations like this, I often use the trick of storing the counter in
>>> the zero element of each array. So, you end up with something like:
>>>
>>> A[++A[0]] = "something"
>> This should not be necessary. Some languages allow
>>
>> A[] = "something"
>>
>> as a way to append to an array, but not AWK. One might think that
>
> Your first say "but not AWK" here.
>
>> A[length(A)] = "something"
>>
>> would do the trick, but calling length before using A as an array turns
>> A into a scalar so you get a run-time error when A is indexed.
>>
>> You can, however, do this:
>>
>> BEGIN { delete A }
>> ...
>> A[length(A)] = "something"
>>
>> which is rather oblique, but works on all the awks I have installed
>> (gawk, nawk, mawk and original-awk). (You can write 'delete A[0]' if
>> you don't want to use the newer POSIX syntax.)
>
> Then you say your suggestion is for AWK here.
>
> So, I'm confused on what do you really mean.

It's a standard code pattern that you find (e.g.) in C/C++ context
(or other languages where you regularly organize your data in arrays
and start indexes counting at 0) but in Awk the ad hoc form of that
pattern is not possible because of its defaults with its variable
types, where you initially need some way to "declare" a variable as
array (to not get an error). So the code pattern is usable, but it
needs - once (in the BEGIN section) - the array type coercion. The
code pattern is generally usable (also in Awk). But note that in
some languages it might be costly (e.g. in C) when the complexity
of length() is not O(1) but O(N). In any case the code pattern makes
explicit and separate index variables unnecessary.

Janis

>
>> --
>> Ben.
>
> Zhao
>

Re: Combine multiple awk calls into one.

<87zg35rwo7.fsf@bsb.me.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6544&group=comp.unix.shell#6544

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Sat, 05 Aug 2023 22:44:56 +0100
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <87zg35rwo7.fsf@bsb.me.uk>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com>
<b44ed684-32e1-409a-a511-ce41b42e2e81n@googlegroups.com>
<39489c84-7306-483d-a3e8-e2a99151152dn@googlegroups.com>
<uailea$18aba$1@dont-email.me> <uaimjh$34hq8$1@news.xmission.com>
<87v8duud6a.fsf@bsb.me.uk>
<0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="1194329fa9ed54d8de1d3edd8e234907";
logging-data="1992839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8vBCr+28/Bdk1cvNHj0FnzotnS6AUsdw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:g2Y5Si7/ng2zFGIzuhu/CDVlygY=
sha1:lNtthmQ5bvPBy/hxLKXGszUBzhU=
X-BSB-Auth: 1.6a78fff900dd93951f28.20230805224456BST.87zg35rwo7.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 5 Aug 2023 21:44 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:

> On Friday, August 4, 2023 at 9:53:25 PM UTC+8, Ben Bacarisse wrote:
>> gaz...@shell.xmission.com (Kenny McCormack) writes:
>>
>> > I note that your general approach is that you have multiple arrays, running
>> > multiple counters (i, i1, etc) - one counter for each array.
>> >
>> > In situations like this, I often use the trick of storing the counter in
>> > the zero element of each array. So, you end up with something like:
>> >
>> > A[++A[0]] = "something"
>> This should not be necessary. Some languages allow
>>
>> A[] = "something"
>>
>> as a way to append to an array, but not AWK. One might think that
>
> Your first say "but not AWK" here.

Yes. AWK does not have the above syntax. Several other scripting
languages do, but not AWK.

>> A[length(A)] = "something"
>>
>> would do the trick, but calling length before using A as an array turns
>> A into a scalar so you get a run-time error when A is indexed.
>>
>> You can, however, do this:
>>
>> BEGIN { delete A }
>> ...
>> A[length(A)] = "something"
>>
>> which is rather oblique, but works on all the awks I have installed
>> (gawk, nawk, mawk and original-awk). (You can write 'delete A[0]' if
>> you don't want to use the newer POSIX syntax.)
>
> Then you say your suggestion is for AWK here.

Yes. I gave a sketch of how to append to an array in AWK, given that
AWK has no simple way to do it like some other scripting languages.

> So, I'm confused on what do you really mean.

Unfortunately I am confused about what is confusing you. I have
obviously not been clear, but I don't know what I need to clarify.

--
Ben.

Re: Combine multiple awk calls into one.

<uamhfk$36fbm$1@news.xmission.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6545&group=comp.unix.shell#6545

  copy link   Newsgroups: comp.unix.shell
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.unix.shell
Subject: Re: Combine multiple awk calls into one.
Date: Sat, 5 Aug 2023 22:12:04 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <uamhfk$36fbm$1@news.xmission.com>
References: <9bcbad5d-5a05-4673-940a-08520d0eb6ban@googlegroups.com> <87v8duud6a.fsf@bsb.me.uk> <0943720a-5a58-4114-aa61-cb01b4db4b9en@googlegroups.com> <87zg35rwo7.fsf@bsb.me.uk>
Injection-Date: Sat, 5 Aug 2023 22:12:04 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3358070"; 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 - Sat, 5 Aug 2023 22:12 UTC

In article <87zg35rwo7.fsf@bsb.me.uk>,
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
....
>Unfortunately I am confused about what is confusing you. I have
>obviously not been clear, but I don't know what I need to clarify.

You're going on and on about a bunch of irrelevant stuff, that has nothing to
do with anything in this thread.

Neither OP nor me have any idea as to why you're doing this, unless you
just got confused about which newsgroup and/or which thread you are posting
to. I understand that people who use "Thunderbird" to read and post to
Usenet often get confused in this way. Apparently, "Thunderbird" gives the
user little information about where their text is going, be it Usenet,
email, or whatever. Maybe this is your problem?

(Yours in Christ...)

--
You are a dreadful man, Kenny, for all your ways are the ways of death.
- Rick C Hodgin -

(P.S. -> https://www.youtube.com/watch?v=sMmTkKz60W8)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor