How-to include a variable's name in a regex in awk to spot command names in absolute paths
Hello all.
I am an absolute beginner in awk. I have an awk script which look into a text file to find if and where some commands are present. I put the name of the command I look for in an awk user variable, let's name that variable thecommand. How to write a regex to find the absolute path of thecommand if mentioned in the text file? Let's say if I look into the third field of each record, for instance. This doesn't work: Code:
$3 ~ /^\/(.+\/)*thecommand/ Or how can I extract the last word from the path, to be able to compare it with thecommand? EDIT. I found a solution (not including the variable's name in the regex but checking that it's the last word of the path): Code:
$3 ~ /^\/(.+\/)*.*/ && length(thecommand) + index($3,thecommand) == length($3)+1 |
// for a standard regex
"" for a computed regex Code:
$3 ~ "^\/(.+\/)*"thecommand |
Thanks grail. IIUC I have to double quote the first part then to concatenate it with the variable (computed as a string) to build the final regex.
I have a warning with your solution : Code:
awk: ligne de commande :1: avertissement : séquence d'échappement « \/ » traitée comme un simple « / » Then I rewrote it this way: Code:
$2 ~ "^/(.+/)*"thecommand"$" I had read, but not really understood, the manual page about dynamic regex. I hope it's only because English is not my native language :o EDIT I eventually appended "$" after thecommand, else a pattern including thecommand as a subdirectory would have matched as well. |
It is a little tricky but you seem to be on to it :)
Also the warning about the escaped slash is the same as other languages where once in double quotes you need to escape the escape if you wish to use it to escape something else. Hence why the final solution works just fine :) |
All times are GMT -5. The time now is 06:44 PM. |