LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2023, 04:03 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Using variables inside regexp constants


Can one introduce variables inside awk regexp constants ?

Code:
gsub(/^[avar]|[bvar]$/, "", str)
 
Old 03-05-2023, 04:11 PM   #2
xode
Member
 
Registered: Aug 2003
Distribution: Mandrake 9.0; FC4; FC8; SUSE 10.3; SUSE 12.1; SUSE 13.2
Posts: 638
Blog Entries: 1

Rep: Reputation: 52
Yes: https://stackoverflow.com/questions/...e-inside-regex But, you do need to be careful about syntax and the like.
 
Old 03-05-2023, 04:39 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
How does it work with awk ?
 
Old 03-06-2023, 12:29 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Not in the / / direct RE context, but in a " " string context.
Code:
gsub(("^" avar "|" bvar "$"), "", str)
With " " you can do string concatenation and all string functions. The disadvantage is the \ must be \ escaped (i.e. doubled) when used in (casted to) a RE context:
\\ in " "
\ in / /

And of course the delimiters must be \ escaped:
backslash" in " "
backslash/ in / /
(Have to write backslash here. I mean \ )

Last edited by MadeInGermany; 03-06-2023 at 12:32 AM.
 
Old 03-06-2023, 12:53 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
that is quite simple, you can use variables:
Code:
# in an awk script
regex_var="adff  fgER gqRg er" # here you can construct the regex as a normal string
gsub(regex_var, "", str)
here is a very good guide: https://learnbyexample.github.io/lea...pressions.html
look for dynamic regexp at the end.
 
Old 03-06-2023, 06:33 AM   #6
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I know how to construct regex string with variable replacements. But cannot do the same with regexp constants, it seems. Is this correct conclusion ?
 
Old 03-06-2023, 08:45 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
I don't understand what do you mean by that.
 
Old 03-06-2023, 01:05 PM   #8
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Meaning that a Dynamic Regex ("drgx") is superior than Constant Regex (/crgx/). Because the former allows variable substitution.

Last edited by Faki; 03-07-2023 at 09:44 AM.
 
  


Reply

Tags
awk



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
Testing a regexp inside awk script Faki Linux - Newbie 1 02-26-2023 01:25 AM
JavaScript: function only taking constants, no variables. :/ RHLinuxGUY Programming 8 01-19-2007 02:49 AM
LXer: Variables Won't Constants Aren't: LXer Syndicated Linux News 0 07-26-2006 04:33 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 01:08 PM
How do you write binary literal constants in C? vasudevadas Programming 5 01-18-2004 07:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 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
Open Source Consulting | Domain Registration