variabile in awk espressione regolare
Domanda
Come passare una variabile di espressione regolare come questo?
match($0, /([^ ]+) (GET|POST|PUT|DELETE) ([^?]+)[^ ]+ HTTP\/[^ ]+ \"(VAR)\" ([^ ]+) ([^ ]+) ([^ ]+)/, matches)
Soluzione
Non sono sicuro di quello che si sta cercando di abbinare, ma se è qualcosa sulla falsariga di:
GETxxxHTTPxxx"VAR"xxx
Credo che si può andare in questo modo:
{
var1="(VAR)"
var="(GET|POST|PUT|DELETE)[x]+(HTTP)[x]+\"" var1 "\"[x]+"
match($0,var,dd);
for (x in dd){
print x,"-->",dd[x]
print"-"
}
}
Quali con l'ingresso sopra, produce il seguente output:
0start --> 1
-
0length --> 21
-
3start --> 15
-
1start --> 1
-
2start --> 7
-
0 --> GETxxxHTTPxxx"VAR"xxx
-
1 --> GET
-
2 --> HTTP
-
3length --> 3
-
3 --> VAR
-
2length --> 4
-
1length --> 3
-
Esecuzione a Ideone qui
Altri suggerimenti
Se si desidera passare una variabile di shell in awk, utilizzare l'opzione -v, in questo modo:
awk -v VAR=${ShellVAR} '{
//you awk program...
}'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow