Jim
Jim

Reputation: 161

awk syntax error

I get this syntax error with my code - I've tried putting the quotes in various places but no luck. Can someone please assist. Thanks!

awk: non-terminated string  | grep Re... at source line 1
 context is
     >>>  <<< 
awk: giving up
 source line number 2
awk '/ (TCP|UDP) / { split($5, addr, /:/); 
cmd = "/bin/geoiplookup " addr[1] | grep 'Rev 1:' | sed 's/Rev 1: //g' " | awk -F', ' '{print $4",", $3",", $2}'; 
cmd | getline rslt; 
close(cmd); 
print $1, $2, $3, $4, $5, $6, rslt }' < "$IP_PARSED" >> "$BlockedIPs"

Upvotes: 3

Views: 5601

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 359925

I think we've been here before. Don't try to do complex processing inside the cmd. Use it to run your external command, then do the processing inside the main AWK program.

awk '/ (TCP|UDP) / { split($5, addr, /:/); 
cmd = "/bin/geoiplookup " addr[1] ; 
while (cmd | getline rslt) {
    if (rslt ~ /Rev 1: /) {
        gsub(/Rev 1: /, "", rslt)
        split(rslt, r, ",")
    }
}
close(cmd); 
print $1, $2, $3, $4, $5, $6, r[4], r[3], r[2] }' < "$IP_PARSED" >> "$BlockedIPs"

Upvotes: 1

geekosaur
geekosaur

Reputation: 61369

You are trying to embed single quotes inside of single quotes. This does not work; moreover, the shell provides no way to easily embed them. There are some complex ways, for example

$ echo 'hello'"'"'world'
hello'world
$ echo 'hello'\''world'
hello'world

Note: embedding \' does not work, as the equivalent does with double quotes.

$ echo 'hello\'world'
hello\'world

Upvotes: 1

ctt
ctt

Reputation: 1435

The argument to awk isn't terminated. awk is interpreting the rest of your pipeline and getting confused. If you actually intend for the pipeline to be part of your awk script, consider either writing your awk script as a file (removing the enclosing single quotes) or replace your outermost single quotes with double quotes and use \ to escape intervening double quotes. Unfortunately, you cannot nest single quotes.

Upvotes: 2

Related Questions