Challenge: to convert what works on the command line into an executable script, putting each string within a file into an array, the file delimeter being empty space of any length.
This works on the command line:
gawk '/searchstring/ {split($0, arr, " "); for (i = 1; i <= NF; i++) { if (arr == "string") {print arr[i+1]}}}' file
This below doesn't work as the executable:
#!/usr/bin/gawk -f
BEGIN {
while ( getline < "file" ) {
split($0, arr, " ")
}
}
for (i = 1; i <= NF; i++) {
if (arr == "string") {
print arr[i+1]
}
}
}
Perhaps it needs to accommodate multidim arrays in the executable... but I'd like to keep it simple.
This works on the command line:
gawk '/searchstring/ {split($0, arr, " "); for (i = 1; i <= NF; i++) { if (arr == "string") {print arr[i+1]}}}' file
This below doesn't work as the executable:
#!/usr/bin/gawk -f
BEGIN {
while ( getline < "file" ) {
split($0, arr, " ")
}
}
for (i = 1; i <= NF; i++) {
if (arr == "string") {
print arr[i+1]
}
}
}
Perhaps it needs to accommodate multidim arrays in the executable... but I'd like to keep it simple.