LinuxQuestions.org
Visit Jeremy's Blog.
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 07-26-2011, 08:28 AM   #1
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Rep: Reputation: Disabled
replace a value in order


I want to change the parameter for ThreadLimit for one file as shown below using sed. I want to ignore the commented line.
But the command I am using also executing with commented line.

# ThreadLimit: maximum setting of ThreadsPerChild
ThreadLimit 25

I want the desired output as

# ThreadLimit: maximum setting of ThreadsPerChild
#ThreadLimit 25
ThreadLimit 50

For this I am using command.

sed '/ThreadLimit/{;h;s/^/#/p;x;s/ .*/ 50/;}' test.txt.1 > test.txt

Please advise.
 
Old 07-26-2011, 08:51 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
So it is not good enough to just change 25 to 50? To fix your intended issue, tell sed the word you are looking for is at the start of the line.
 
Old 07-26-2011, 12:26 PM   #3
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Original Poster
Rep: Reputation: Disabled
There is issue in doing so. The value is not fixed, it can be any value (eg . 10 , 15 or 19)
I just want to replace the value to 50 irrespective of the existing value.
 
Old 07-26-2011, 12:55 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try this:
Code:
sed -i.bak '/^ThreadLimit/ s/^/#/' filename
With the advice that has been given to you in your previous threads you should be able to append another line.
 
Old 07-26-2011, 12:59 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As grail said, anchor the address match to the beginning of the line in order to only affect lines that start with "ThreadLimit".

But the command can be made much simpler in any case.

Code:
sed -r '/^ThreadLimit/ s/^(.*)/#\1\nThreadLimit 50/' file
Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
 
Old 07-26-2011, 03:19 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Or to take David's advice and only change any number:
Code:
sed '/^ThreadLimit/s/[0-9]+$/50/' file
 
Old 07-26-2011, 06:50 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I'd use
Code:
sed -e 's|^\([\t ]*ThreadLimit[\t ]\+\)\([-+]*[0-9]\+\)\(.*\)$|#&\n\1'"50"'|' -i file
For clarity, I separated the new parameter, 50, into a double-quoted part, so you can use e.g. a shell variable reference instead of a fixed constant.

The first subpattern will match optional whitespace, the keyword, and trailing whitespace. The second subpattern will match an integer parameter. The third subpattern will match everything trailing the parameter, if anything. The old line will be commented out, with the new one immediately following the commented out line.

If there are more than one ThreadLimit keyword (as the first token on a line, not commented out), the above will apply to each and every one.

Last edited by Nominal Animal; 07-26-2011 at 06:53 PM.
 
Old 07-27-2011, 11:13 PM   #8
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Original Poster
Rep: Reputation: Disabled
Thanks so much .. Nominal ..
It worked.
 
  


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] Can I set the cups output order to always print in reverse order Thane Ubuntu 3 07-09-2018 09:49 PM
System suddenly changing disk order from BIOS order hscast Fedora 2 02-16-2010 09:09 PM
How to replace UTF-8 BOMs (Byte Order Marks) () in a File robbbert Linux - General 1 05-01-2008 03:40 AM
Replace Autopartition force partition order! ROCKS Cluster Ricio ROCK 0 04-27-2008 02:40 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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