Need script to change a line in file....

Hello all,

I have a line of code in a file that I need to change in the /etc/sysconfig/kdump file
presently the line reads:

KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"

what I need to do is put a comment out the 1st line and repeat it, and change nr_cpus=1 to maxcpus=1.
I am looking for the final result to look like:

# KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"
KDUMP_COMMANDLINE_APPEND="irqpoll maxcpus=1 reset_devices cgroup_disable=memory mce=off"

thanks,

really appreciate any assistance.

sed '
s/\(.*\) nr_\(cpus=1.*\)/# &\
\1 max\2/
 ' in_file > out_file
$ cat temp.x
Preceding line
KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"
Following line
$ cat test.sed
/KDUMP_COMMANDLINE_APPEND/ {
  h                   # hold original line
  s/.*/# &/           # Add comment
  p                   # print commented line
  g                   # retrieve original line
  s/nr_cpus/maxcpus/  # change the text
  }
$ sed -f test.sed temp.x
Preceding line
# KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"
KDUMP_COMMANDLINE_APPEND="irqpoll maxcpus=1 reset_devices cgroup_disable=memory mce=off"
Following line
$ sed "/KDUMP_COMMANDLINE_APPEND/ {h; s/.*/# &/; p; g; s/nr_cpus/maxcpus/}" temp.x
Preceding line
# KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"
KDUMP_COMMANDLINE_APPEND="irqpoll maxcpus=1 reset_devices cgroup_disable=memory mce=off"
Following line

Use of hold space is nice, but you are commenting out lines that do not change.

We do need a tighter definition of the class of lines to be changed, unless it is just the one.

I agree. I was just trying to follow the skimpy example input / output.

Small changes! The cost of scanning for nr_cpus twice is offset by the simpler substitute -- 's/\(...\).../\1.../' is not so cheap:

/KDUMP_COMMANDLINE_APPEND.*nr_cpus/ {
  h                   # hold original line
  s/^/# /             # Add comment
  p                   # print commented line
  g                   # retrieve original line
  s/nr_cpus/maxcpus/  # change the text
 }