LinuxQuestions.org
Help answer threads with 0 replies.
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 08-27-2018, 03:33 PM   #1
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Rep: Reputation: Disabled
Using sed to substitute different values


Good day.

I've been trying to find a method to substitute values strings of different lengths using sed. Using grep I search for an entry followed by an equal sign after which I want to replace all digits or letters.

Shown below, in my case I want to change MAXDAYS=300 to MAXDAYS=90, however the original could be anything.

I have tried the following:

Code:
sed -i -e 's/^MAXDAYS=[0-9]/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS=[0..9]/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS={?}/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS={.}/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS=./MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS=*/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS={*}/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS=\{.\}/MAXDAYS=90/' SystemHardening.cfg
sed -i -e 's/^MAXDAYS=\{*\}/MAXDAYS=90/' SystemHardening.cfg
Any hints or tips appreciated.

Bjoern
 
Old 08-27-2018, 03:50 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
test file
Code:
MAXDAYS=900
MAXDAYS=20000
sed
Code:
sed -i -e 's|MAXDAYS=.*|MAXDAYS=90|' maxdays
results
Code:
MAXDAYS=90
MAXDAYS=90
 
1 members found this post helpful.
Old 08-27-2018, 05:23 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The following avoids the repetition of the search key
Code:
sed -i '/^MAXDAYS=/ s/=.*/=90/' SystemHardening.cfg
 
Old 08-27-2018, 06:40 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Better in both cases to use [:alnum:] class to ensure the original request is properly resolved. Also retains any following data - say a comment - if it exists.
 
1 members found this post helpful.
Old 08-27-2018, 06:50 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by MadeInGermany View Post
The following avoids the repetition of the search key
Code:
sed -i '/^MAXDAYS=/ s/=.*/=90/' SystemHardening.cfg
MadeInGermany...would you mind explaining that syntax...new stuff for me. I'd have done it the way BW-userx did.
 
Old 08-27-2018, 06:55 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by syg00 View Post
Better in both cases to use [:alnum:] class to ensure the original request is properly resolved. Also retains any following data - say a comment - if it exists.
I get how that's more specific than .*, but given the use case, shouldn't it be [:digit:]...the OP only wants to replace numbers.

Not criticizing...asking to learn.
 
Old 08-27-2018, 07:01 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Read the OP.
 
Old 08-27-2018, 07:03 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I'd think it'd still need to have that requirement of "MAXDAYS" or even just the "=" for the search pattern , and not just numbers being all changed to 90.
 
Old 08-27-2018, 07:34 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by syg00 View Post
Read the OP.
Ahh!
Quote:
replace all digits or letters
Got it.
 
Old 08-27-2018, 10:44 PM   #10
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
test file
Code:
MAXDAYS=900
MAXDAYS=20000
sed
Code:
sed -i -e 's|MAXDAYS=.*|MAXDAYS=90|' maxdays
results
Code:
MAXDAYS=90
MAXDAYS=90
Thank you BW, works like a charm.

Bjoern
 
1 members found this post helpful.
Old 08-28-2018, 12:55 AM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
You can mark this thread "Solved" using the Thread Tools at the top of the thread.
 
Old 08-29-2018, 08:20 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by scasey View Post
MadeInGermany...would you mind explaining that syntax...new stuff for me. I'd have done it the way BW-userx did.
Code:
/key/ s/old/new/
substitutes old by new if key is found.
 
1 members found this post helpful.
  


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] sed substitute one of two strings keirvt Linux - Software 1 10-01-2015 06:17 PM
[SOLVED] substitute ( in sed ghantauke Programming 7 03-11-2011 10:46 AM
sed : substitute without displacing columns billywayne Programming 13 05-27-2010 08:33 PM
[SOLVED] sed substitute everything until character sqn Programming 5 03-30-2010 10:27 AM
Sed substitute for my username? camaroblue87 Linux - Newbie 1 04-29-2006 11:07 AM

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

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