LinuxQuestions.org
Visit Jeremy's Blog.
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, 04:33 PM   #1
brh1
LQ Newbie
 
Registered: Sep 2012
Location: Texas
Posts: 24

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, 04:50 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,405

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
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, 06:23 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,249

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

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
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, 07:50 PM   #5
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,101

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
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, 07:55 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,101

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
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, 08:01 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,307

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
Read the OP.
 
Old 08-27-2018, 08:03 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,405

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
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, 08:34 PM   #9
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,101

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

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, 01:55 AM   #11
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,101

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

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


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

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

All times are GMT -5. The time now is 01:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration