Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

It's computer hardware, of course it's worth having <g> -- Espy on #Debian


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

Re: Combine multiple awk calls into one.

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

  copy mid

http://rslight.i2p/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)
}

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

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

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

Then, test as follows:

werner@X10DAi:~/awk-vasprun$ ./awk.1 dft
werner@X10DAi:~/awk-vasprun$ ls *dat | while read -r filename; do echo "File: $filename"; cat "$filename"; echo; done
File: optics-current-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

File: optics-density-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

File: optics-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

werner@X10DAi:~/awk-vasprun$ ./awk.2 dft
werner@X10DAi:~/awk-vasprun$ ls *dat | while read -r filename; do echo "File: $filename"; cat "$filename"; echo; done
File: optics-current-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

File: optics-density-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

File: optics-dft.dat
0.0000 0 0.0089
0.0200 0.003 0.0119
0.0400 0.0059 0.0149

Best,
Zhao

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

SubjectRepliesAuthor
o Combine multiple awk calls into one.

By: hongy...@gmail.com on Wed, 2 Aug 2023

17hongy...@gmail.com
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor