Search code examples
htmlbashawk

Trying to figure out why bash code with awk/read doesn't work on another device


I'm trying to figure out why code below doesn't work on another device. Any ideas what can be the problem?

The file "search" is an html code with multiple href entries:

<a class="movieTitleCat" tytul="Godzilla: Król potworów"  id="14220" href="napisy-14220-Godzilla-Król-potworów-(1954)"><h3>Godzilla: Król potworów (Gojira) - 1954r. </h3></a>

Script:

#!/bin/bash
read -r -d "" awkCode << 'SEARCHTITLEAWKEOF'
/movieTitleCat/ {
    match($0, /href="([^"]*)"/, cHref)
    printf("%s\n", (napiBase "/" cHref[1]))
}
SEARCHTITLEAWKEOF

cat /home/gato/search | awk -v napiBase="http://lalalala.com" "$awkCode"


$ bash ttt.sh 
http://lalalala.com/napisy-14220-Godzilla-Król-potworów-(1954)
http://lalalala.com/napisy-6336-Godzilla-(1984)
http://lalalala.com/napisy-17864-Godzilla-kontra-Gigan-(1972)
http://lalalala.com/napisy-12747-Powrót-Godzilli-(1999)
http://lalalala.com/napisy-21140-Godzilla-kontra-Mothra-(1992)
http://lalalala.com/napisy-21144-Godzilla-kontra-Kosmogodzilla-(1994)
http://lalalala.com/napisy-14424-Godzilla-kontra-Megaguirus-(2000)
http://lalalala.com/napisy-11423-Godzilla-kontra-Destruktor-(1995)
http://lalalala.com/napisy-17799-Godzilla-kontra-Hedora-(1971)
http://lalalala.com/napisy-19933-Godzilla-kontra-Mechagodzilla-2-(1993)
http://lalalala.com/napisy-18110-Godzilla-kontra-Mothra-(1964)
http://lalalala.com/napisy-3389-Godzilla-kontra-Mechagodzilla-(1974)
http://lalalala.com/napisy-20161-Godzilla-Mothra-król-Ghidora-Gigantyczne-potwory-atakują-(2001)
http://lalalala.com/napisy-21282-Godzilla-kontra-Mechagodzilla-III-(2002)
http://lalalala.com/napisy-55497-Godzilla-II-Król-potworów-(2019)

and here it doesn't work on another device, none strings after href="

bash /home/pm/tttt.sh 
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
http://lalalala.com/
bash --help
GNU bash, version 4.4.0(4)-release-(aarch64-unknown-linux-gnu)

Solution

  • The reason your code doesn't work an another device isn't due to a different version of Bash but a smaller implementation of awk. If you ran awk in your script with --posix argument like that:

    cat search | awk --posix -v napiBase="http://lalalala.com" "$awkCode"
    

    it would say:

    awk: cmd. line:2:     match($0, /href="([^"]*)"/, cHref)
    awk: cmd. line:2:                                      ^ match: third argument is a gawk extension
    

    You need to change the AWK lines that refers to an array:

    match($0, /href="([^"]*)"/, cHref)cHref[1]))
    printf("%s\n", (napiBase "/" cHref[1]))
    

    with this:

    match($0, /href="([^"]*)"/)
    printf("%s\n", (napiBase "/" substr($0, RSTART + 6, RLENGTH - 7)))