I'm writing a script that replaces a value in a file. The file is formatted as follows:
[param_one] [value]
[param_two] [value]
So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the output to new_file.cfg. $p2 is a variable passed as an arg on the command line. Here's what I have:
sed -e "/^\[param_two/s/[0-9]/$p2/" file.cfg > new_file.cfg
This code works like a charm if the value for param_two is a one-digit number and $p2 is a one or two-digit number. If the value is a two-digit number, however, my code only replaces only the first digit; which is not what I want. I want to be able to replace the value with whatever is passed for $p2. Any ideas? Thanks in advance.
Sure. You can create file.cfg with these contents:
[param_one] [/usr/local/foo]
[param_two] [23]
Then make a script with this code:
sed -e '/^\[param_two/s/[0-9]/6/' file.cfg > new_file.cfg
What I'm hoping for here is to replace the value [23] with [6] (Note: I removed the command line arg/variable from earlier, as I'm just trying to make this as simple an example as possible). Thanks a lot.
Thanks a lot! This works exactly like I need it to. For my own edification, what does the new \{1,2\} portion of the regex mean in english? I really appreciate the help.
[0-9]{\1,2\} = 1 or 2 of [0-9]
[0-9]{\1,3\} = 1 to 3 of [0-9]
[0-9]{\1,6\} = 1 to 6 of [0-9]
[0-9]{\3,6\} = 3,4,5 or 6 of [0-9]
[0-9]{\3,\} = 3 or more of [0-9]
[0-9]\+ = 1 or more of [0-9]