admin管理员组

文章数量:1418673

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="; "$awkCode"


$ bash ttt.sh 
(1954)
(1984)
(1972)
(1999)
(1992)
(1994)
(2000)
(1995)
(1971)
(1993)
(1964)
(1974)
(2001)
(2002)
(2019)

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

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

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" "$awkCode"


$ bash ttt.sh 
http://lalalala/napisy-14220-Godzilla-Król-potworów-(1954)
http://lalalala/napisy-6336-Godzilla-(1984)
http://lalalala/napisy-17864-Godzilla-kontra-Gigan-(1972)
http://lalalala/napisy-12747-Powrót-Godzilli-(1999)
http://lalalala/napisy-21140-Godzilla-kontra-Mothra-(1992)
http://lalalala/napisy-21144-Godzilla-kontra-Kosmogodzilla-(1994)
http://lalalala/napisy-14424-Godzilla-kontra-Megaguirus-(2000)
http://lalalala/napisy-11423-Godzilla-kontra-Destruktor-(1995)
http://lalalala/napisy-17799-Godzilla-kontra-Hedora-(1971)
http://lalalala/napisy-19933-Godzilla-kontra-Mechagodzilla-2-(1993)
http://lalalala/napisy-18110-Godzilla-kontra-Mothra-(1964)
http://lalalala/napisy-3389-Godzilla-kontra-Mechagodzilla-(1974)
http://lalalala/napisy-20161-Godzilla-Mothra-król-Ghidora-Gigantyczne-potwory-atakują-(2001)
http://lalalala/napisy-21282-Godzilla-kontra-Mechagodzilla-III-(2002)
http://lalalala/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/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
http://lalalala/
bash --help
GNU bash, version 4.4.0(4)-release-(aarch64-unknown-linux-gnu)
Share Improve this question edited Jan 29 at 19:09 D S asked Jan 29 at 18:59 D SD S 1295 bronze badges 8
  • Is the call to tttt.sh a typo (e.g. 4 T's) and can you explain where exactly this isn't working? – scunliffe Commented Jan 29 at 19:03
  • the script just has a different name on a different device. – D S Commented Jan 29 at 19:05
  • 2 Your Awk code isn't standard (a match with three arguments is a GNU extension). BTW you should use a XPath processor for this task; that would be as simple as //a[@class="movieTitleCat"]/@href – Fravadona Commented Jan 29 at 19:17
  • "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> – D S Commented Jan 29 at 19:18
  • 1 I think it's a matter of awk as mentioned, I ran your code in bash:4.4.0 image and it produces expected output but only after GNU awk is installed – Arkadiusz Drabczyk Commented Jan 29 at 19:20
 |  Show 3 more comments

2 Answers 2

Reset to default 4

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" "$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)))
#!/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" "$awkCode"

From String Functions

match(string, regexp [, array])

(...)

The array argument to match() is a gawk extension.(...)

so you need GNU AWK for use said script. If it is available, but is not default, it should be sufficient ro replace

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

using

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

Otherwise you would need to rework your command. Nonetheless observe that awk is poorly suited for processing HTML and if possible use HTML-aware tool for extracting data from HTML document, e.g. hxselect

本文标签: htmlTrying to figure out why bash code with awkread doesn39t work on another deviceStack Overflow