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. |
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.
|
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. |
Try this:
Code:
sed -i.bak '/^ThreadLimit/ s/^/#/' filename |
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 |
Or to take David's advice and only change any number:
Code:
sed '/^ThreadLimit/s/[0-9]+$/50/' file |
I'd use
Code:
sed -e 's|^\([\t ]*ThreadLimit[\t ]\+\)\([-+]*[0-9]\+\)\(.*\)$|#&\n\1'"50"'|' -i file 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. |
Thanks so much .. Nominal ..
It worked. |
All times are GMT -5. The time now is 06:55 AM. |