LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Make gawk line work in mawk (https://www.linuxquestions.org/questions/programming-9/make-gawk-line-work-in-mawk-4175641744/)

rbees 11-04-2018 08:58 AM

Make gawk line work in mawk
 
Thanks for helping thick headed people like me.

I have a line in a script that works fine in gawk but fresh installs of debian based systems come with mawk instead. On a desktop or laptop that is not an issue really because storage space and cpu power are plentiful. But I wish to use this script on a Pi where storage space and cpu power are in limited supply.

So I would like to change this line to work with mawk, and gawk if possible.

Code:

BLOCKHEAD=$(awk "/\y${data[1]}\y/ {print FNR}" $TMPDIR/tempfile1b)
It fails with this output

Code:

+ w3m -dump -T text/html /home/kingbee/bin/shabbat/data/JPS/et0125.htm
+ sed 1,5d
+ sed -e :a -e '$d;N;2,6ba' -e 'P;D'
+ ((  19 > 1 ))
++ awk '/\y19\y/ {print FNR}' /home/kingbee/bin/shabbat/tmp/tempfile1b
+ BLOCKHEAD=
+ BLOCKHEAD=-1
+ sed -i '1,-1 d' /home/kingbee/bin/shabbat/tmp/tempfile1b
sed: -e expression #1, char 3: unexpected `,'
++ sed -n -e '1 s/^.*19/19/p' /home/kingbee/bin/shabbat/tmp/tempfile1b
+ FIRSTLINE=

With gawk it outputs

Code:

+ w3m -dump -T text/html /home/rbees/bin/shabbat/data/JPS/et0125.htm
+ sed 1,5d
+ sed -e :a -e '$d;N;2,6ba' -e 'P;D'
+ ((  19 > 1 ))
++ awk '/\y19\y/ {print FNR}' /home/rbees/bin/shabbat/tmp/tempfile1b
+ BLOCKHEAD=31
+ BLOCKHEAD=30
+ sed -i '1,30 d' /home/rbees/bin/shabbat/tmp/tempfile1b
++ sed -n -e '1 s/^.*19/19/p' /home/rbees/bin/shabbat/tmp/tempfile1b
+ FIRSTLINE='19 And these are the generations of Isaac, Abraham'\''s son: Abraham begot Isaac. '
+ sed -i '1s/.*/19 And these are the generations of Isaac, Abraham'\''s son: Abraham begot Isaac. /' /home/rbees/bin/shabbat/tmp/tempfile1b

mawk on pi mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

gawk on Desktop GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2016 Free Software Foundation.

From what I have read if the versions of mawk and gawk are the same then they are compatible but clearly my versions are not the same.

The desktop is running Wheezy (stable) the pi is running raspbian wheezy.

So is it doable or should I just install gawk?

Note: there are other lines in the script that may fail when conditions hit them but at this point I am not aware of them.

Thanks again for your time.

lougavulin 11-04-2018 10:18 AM

I don't have mawk on my system, but
Code:

\y
is specific to gawk, as
Code:

\B, \<, \>, \s, \S, \w, \W, \`, and \'
To test your 'awk' script for both, run gawk with '--posix' option.

MadeInGermany 11-04-2018 01:24 PM

Try the more commonly supported \< and \> word boundaries:
Code:

BLOCKHEAD=$(awk "/\<${data[1]}\>/ {print FNR}" $TMPDIR/tempfile1b)

rbees 11-04-2018 05:18 PM

Thanks lougavulin and MadeInGermany

You gave me what I needed to look for in the man. Comparing what the script actually dose, not that I really remember why, there are extensive changes that have to be made for it to actually work with mawk. That will require to much work that is beyond my programming ability and so will take a lot of time that I can better use for other more important things.

So I install gawk

Thanks again Linux Question users


All times are GMT -5. The time now is 05:44 AM.