LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2012, 04:06 PM   #1
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
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/
Should I quote thecommand in some way, or what?

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
PS Still, I would be interested to know how to include a variable's name in a regex...

Last edited by Didier Spaier; 09-13-2012 at 05:52 PM. Reason: regex corrected
 
Old 09-13-2012, 09:57 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
// for a standard regex

"" for a computed regex

Code:
$3 ~ "^\/(.+\/)*"thecommand
Reference: http://www.gnu.org/software/gawk/man...mputed-Regexps

Last edited by grail; 09-13-2012 at 09:58 PM.
 
1 members found this post helpful.
Old 09-13-2012, 11:54 PM   #3
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Original Poster
Rep: Reputation: Disabled
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 « / »
though that doesn't seem to hurt. Sorry that the warning be in French, it's gettext's fault

Then I rewrote it this way:
Code:
$2 ~ "^/(.+/)*"thecommand"$"
and it works as well, with no warning.

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

EDIT I eventually appended "$" after thecommand, else a pattern including thecommand as a subdirectory would have matched as well.

Last edited by Didier Spaier; 09-14-2012 at 05:42 PM. Reason: Text beginning with EDIT added.
 
Old 09-14-2012, 04:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 02:35 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 08:38 AM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 11:31 AM
/usr/include/regex.h errors? gbowden Slackware 0 07-03-2007 11:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration