'AWK: Pattern scanning debug script not working

I have the following table:

cat test.txt 
c_az_1858   2020-01-15  -5.50   Parking Serv        Parking Serv
c_az_1859   2020-01-15  -80.56  Avery Johnson   Avery Johnso    592242
c_az_1860   2020-01-15  100.00  Wayne Alexander Flin    7 Pikarere S    Titahi Bay
c_az_1861   2020-01-15  51.75   Setefano P M    Crew Cuts   Lawns
c_az_1862   2020-01-13  -5.50   Parking Serv        Parking Serv
c_az_1863   2020-01-13  -3.00   Parking Serv        Parking Serv
c_az_1864   2020-01-13  57.50   0520/5200000000/002     Apu Cresent
c_az_1865   2020-01-13  46.00   Becta Ltd   Taylormallon    Lawns
c_az_1866   2020-01-13  28.75   Strata Title Adminis    Crewcut Gard    De Payment
c_az_1867   2020-01-13  19.17   D S & S A Tapp  David Tapp  Weekly Lawn

I am trying to run a series of search patterns against the file so it prints out the search pattern that calls the line in front of the line. The search pattern scans column $4. Like so:

Park: c_az_1858 2020-01-15      -5.50   Parking Serv            Parking Serv
ayn : c_az_1860 2020-01-15      100.00  Wayne Alexander Flin    7 Pikarere S    Titahi Bay
o P: c_az_1861  2020-01-15      51.75   Setefano P M    Crew Cuts       Lawns
Park: c_az_1862 2020-01-13      -5.50   Parking Serv            Parking Serv
Park: c_az_1863 2020-01-13      -3.00   Parking Serv            Parking Serv
S A: c_az_1867  2020-01-13      19.17   D S & S A Tapp  David Tapp      Weekly Lawn

To this end I have written the following script:

#!/usr/bin/env bash
  
awk '
BEGIN{
        FS = OFS = "\t"
        x="ayn|o P|S A|Park"
}
{
for (i in x) {
        if ($4 ~ i) {
                print x[i] ": " , i 
        }
}
}
' test.txt

When I run this I get the following error message:

awk: cmd. line:7: (FILENAME=test.txt FNR=1) fatal: attempt to use scalar `x' as an array

How is x a scalar and how would this have to be rewritten so it work. Help is much appreciated.

awk


Solution 1:[1]

Another option could be changing the pipe delimited string to a regex by replacing the double quotes with forward slashes, where the pipes in the pattern will be used to list the alternatives.

Then you could check for a match in column 4 and print the first matched part plus the whole line.

awk '
BEGIN{FS=OFS="\t"}
match($4, /ayn|o P|S A|Park/) { 
  print substr($4, RSTART, RLENGTH) ":", $0
}
' test.txt

Output

Park:   c_az_1858       2020-01-15      -5.50   Parking Serv            Parking Serv
ayn:    c_az_1860       2020-01-15      100.00  Wayne Alexander Flin    7 Pikarere S    Titahi Bay
o P:    c_az_1861       2020-01-15      51.75   Setefano P M    Crew Cuts       Lawns
Park:   c_az_1862       2020-01-13      -5.50   Parking Serv            Parking Serv
Park:   c_az_1863       2020-01-13      -3.00   Parking Serv            Parking Serv
S A:    c_az_1867       2020-01-13      19.17   D S & S A Tapp  David Tapp      Weekly Lawn

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The fourth bird