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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to change a line in file....
# 1  
Old 03-20-2013
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:

Code:
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:

Code:
# 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.

Last edited by Scrutinizer; 03-20-2013 at 07:18 PM.. Reason: code tags; formatting discarded
# 2  
Old 03-20-2013
Code:
sed '
s/\(.*\) nr_\(cpus=1.*\)/# &\
\1 max\2/
 ' in_file > out_file


Last edited by DGPickett; 03-20-2013 at 07:20 PM..
# 3  
Old 03-20-2013
Code:
$ cat temp.x
Preceding line
KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off"
Following line

Code:
$ 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
  }

Code:
$ 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

Code:
$ 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

# 4  
Old 03-22-2013
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.
# 5  
Old 03-23-2013
I agree. I was just trying to follow the skimpy example input / output.
# 6  
Old 03-25-2013
Small changes! The cost of scanning for nr_cpus twice is offset by the simpler substitute -- 's/\(...\).../\1.../' is not so cheap:
Code:
/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
 }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change file section into each line?

Hi Gurus, I have below file which has different sections, need to move the sections to beginning of the each record. original file aaa bbb ccc ddd eee fff output file. aaa bbb ccc ddd eee fff (6 Replies)
Discussion started by: green_k
6 Replies

2. UNIX for Beginners Questions & Answers

How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below test-000000 test-000001 #and so on until 14 now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text? sample... (5 Replies)
Discussion started by: Sekhar419
5 Replies

3. Shell Programming and Scripting

Script to change first line of files in directory

I need a script to take the filename of every file in a directory and substitute that file name for whatever is on the first line of the file. There may or may not be anything on the line, but I want the line to be the same as the file name. Most of the script tools I have used are non-destructuve,... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

4. Shell Programming and Scripting

Change in last line of each part of a file

Hi, I have a input files as below. Sample of one input file type mtg_key = record decimal("\344") id; string("\344") ct_cd; string("\344") st_cd; end; type psl_key = record decimal("\344") id; utf8 string("\344") st_cd; end; type amk_fields = record ... (6 Replies)
Discussion started by: adgangwar
6 Replies

5. Shell Programming and Scripting

Change a line in a php file thanks to a shell script

Hi, I'm working on a script to make automatic the new releases of my website... However in this script I put all the css script in a single one. There's no rpoblem for that. My problem is when I want to change the header of my layout page to put instead of : $header.="<link rel=\"stylesheet\"... (2 Replies)
Discussion started by: lahabana
2 Replies

6. Shell Programming and Scripting

How to change values in certain column only in every line (any script)

Let say in a file I have lines of data like this : 13;2073;461496;15075341;3;001f7d3a;2042063674; 13;2074;461446;15080241;6;001ed33a;2042020154; 13;2075;461401;15085270;6;001f593b;2042054459; 13;2076;461381;15087160;6;001f7483;2042061443; 13;2077;461419;15083419;6;001eca1a;2042017818; I... (3 Replies)
Discussion started by: luna_soleil
3 Replies

7. Shell Programming and Scripting

Need to change two words in a line using shell script.

Hi, i have a line tftp dgram udp wait nobody /usr/sbin/tcpd in.tftpd /tftpboott in /etc/inet.conf file. ineed to replace nobody with root and /tftpboott with /flx/boot. i tried using sed ,but i could not change both of them. can you please help me to do this. Edit:... (7 Replies)
Discussion started by: vprasads
7 Replies

8. Shell Programming and Scripting

how to change one line in a file?

Suppose I have a file. The content of the file is: Fwf33 #enable_a STH. fwqfw egvega What I want to do is: if "STH" = "YES", do not change it, otherwise, change it to "YES". e.g. if “enable_a ERR” change it to be “enable_a YES” if “enable_a YES” do not change the file. ... (2 Replies)
Discussion started by: lilili07
2 Replies

9. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

10. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies
Login or Register to Ask a Question