LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 05:06 PM   #1
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,676

Rep: Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237
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 06:52 PM. Reason: regex corrected
 
Old 09-13-2012, 10:57 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
// 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 10:58 PM.
 
1 members found this post helpful.
Old 09-14-2012, 12:54 AM   #3
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,676

Original Poster
Rep: Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237Reputation: 1237
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 06:42 PM. Reason: Text beginning with EDIT added.
 
Old 09-14-2012, 05:05 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:35 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 09:38 AM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 12:31 PM
/usr/include/regex.h errors? gbowden Slackware 0 07-03-2007 12:03 PM


All times are GMT -5. The time now is 12:55 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration