LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-14-2021, 04:36 AM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Regex - Modify Parameter in config file


Hi There,

I'd like to modify a parameter in a config file using a regex
Sample config file:
Code:
[ServiceXY]
ParamXY=true
Enable=False

[Service123]
ParamXY=true
Enable=False

[Service345]
ParamXY=true
Enable=False

[Service456]
ParamXY=true
Enable=False
My goal is to modify the "Enable=False" of the section "Service345" to "Enable=True"

The following work fine in Regex101.com
Match
Code:
/(\[Service345\]\r?\n.*(\r?\n([^[\r\n].*)?)*Enable=)False/
And use this as substitution
Code:
/$1True/

But when I try to use it in command line it doesn't work:
Code:
perl -pe 's/(\[Service345\]\r?\n.*(\r?\n([^[\r\n].*)?)*Enable=)False/\1/gm' test.conf
Any Idea why it doesn't substitute in command line?

Thanks for your help.

Last edited by angel115; 09-14-2021 at 05:39 AM. Reason: Fix Mistake
 
Old 09-14-2021, 04:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
the name of the section is Service345 or Section345 ?
 
Old 09-14-2021, 05:22 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Probably easier done - and more readable - in awk or sed. Might help debugging errors too.
 
Old 09-14-2021, 05:40 AM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Original Poster
Rep: Reputation: 79
Quote:
Originally Posted by pan64 View Post
the name of the section is Service345 or Section345 ?
Service, I just fix it in the original post
 
Old 09-14-2021, 05:43 AM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Original Poster
Rep: Reputation: 79
Quote:
Originally Posted by syg00 View Post
Probably easier done - and more readable - in awk or sed. Might help debugging errors too.
I've tried as well with sed but got the same result.
sed -E 's/(\[Service345\]\r?\n.*(\r?\n([^[\r\n].*)?)*Enable=)False/\1True/gm' test.conf
 
Old 09-14-2021, 05:47 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
using sed you can do something like this:
Code:
sed '/Service345/,/^[[]/s/Enable=False/Enable=True/' file
 
1 members found this post helpful.
Old 09-14-2021, 05:53 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Or even this if you can assure the linecount
Code:
sed '/\[Service345\]/{N;N;s/False$/True/}' file

Last edited by syg00; 09-14-2021 at 05:57 AM. Reason: typo
 
Old 09-14-2021, 06:20 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Quote:
Originally Posted by angel115 View Post
Any Idea why it doesn't substitute in command line?
It's still reading one line at a time, using newlines or whatever for the line separators. Try clearing the input record separator or else assigning it to null or something strange:

Code:
perl -0 -p -e 's/^(\[Service345\][\r\n\s\w=]*)Enable=False/$1Enable=True/m' test.conf
perl -0777 -p -e 's/^(\[Service345\][\r\n\s\w=]*)Enable=False/$1Enable=True/m' test.conf

perl -p -e 'undef $/; s/^(\[Service345\][\r\n\s\w=]*)Enable=False/$1Enable=True/m' test.conf
See "man perlrun" and "man perlvar". The former for -0 and the latter for $INPUT_RECORD_SEPARATOR aka $RS aka $/

Edit:

Code:
perl -p -e 'BEGIN{ $/=qq(\n\n);} s/^(\[Service345\][\r\n\s\w=]*)Enable=False/$1Enable=True/m'  test.conf
and so on ...

Last edited by Turbocapitalist; 09-14-2021 at 06:34 AM. Reason: g not needed
 
1 members found this post helpful.
Old 09-14-2021, 06:25 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Now, about that readability ...
 
Old 09-14-2021, 06:32 AM   #10
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,573

Rep: Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534
Quote:
Originally Posted by angel115 View Post
But when I try to use it in command line it doesn't work:
Code:
perl -pe 's/(\[Service345\]\r?\n.*(\r?\n([^[\r\n].*)?)*Enable=)False/\1/gm' test.conf
Any Idea why it doesn't substitute in command line?
The answer to the question you've asked is that you're trying to use a multi-line pattern but are splitting the input on newlines.

In Perl you can use -0 (that's zero, not O) to change the delimiters to ASCII NUL character instead of newline, and thus treat the entire input as a single record.

Adding that would make your pattern work, but I recommend removing/ignoring carriage returns to simplify things, and being either more flexible with your non-relevant lines, or more explicit. Also, you were missing the ^$ bounds that make the m flag useful, and only need g flag if there could be multiple instances to replace, so...

Code:
perl -p0e 's/(^\[Service345\](?:\n(?!\[).*)*?\nEnable=)False$/\1True/m' input.config
Code:
perl -p0e 's/(^\[Service345\]\nParamXY=.*\nEnable=)False$/\1True/m' input.config

If doing this with sed, a corrected version of Pan's expression would be:
Code:
sed '/^\[Service345\]/,/^[[]/s/^Enable=False$/Enable=True/' file

Last edited by boughtonp; 09-14-2021 at 06:39 AM.
 
2 members found this post helpful.
Old 09-15-2021, 12:51 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Assuming that this is not a one-off, and going with syg00's comment about 'readability', which I totally agree with, frankly I'd actually write a short Perl program instead.
The result would be much more readable / maintainable.
 
Old 09-15-2021, 01:41 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,768

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Code:
awk '
  BEGIN {FS=OFS="="}
  f && $1=="Enable" {$2="True"}
  /\[/ {f=0}
  /\[Service345\]/ {f=1}
' file
 
  


Reply

Tags
configuration, regex


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 03:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:21 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