Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

editing options in a file

by rjsaulakh (Beadle)
on Jun 21, 2006 at 13:01 UTC ( #556646=perlquestion: print w/ replies, xml ) Need Help??
rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

i have a config file in my /usr/src/linux2.4 . the config file have some configuration entries in it along with the option as y, m or n

# General setup # CONFIG_NET=y CONFIG_HOTPLUG=y CONFIG_BSD_PROCESS_ACCT=y CONFIG_NR_CPUS=8 CONFIG_MAX_USER_RT_PRIO=100 CONFIG_MAX_RT_PRIO=0 + # # Busses # + # CONFIG_MCA is not set CONFIG_PCI=y CONFIG_PCI_NAMES=y # CONFIG_HOTPLUG_PCI is not set # CONFIG_HOTPLUG_PCI_COMPAQ_NVRAM is not set # CONFIG_HOTPLUG_PCI_IBM is not set

i have to write a perl script to edit this file .and the changes i have to make are fixed . so can you please help me how can i edit this file through my script and i can set or unset the various parameters

Comment on editing options in a file
Download Code
Re: editing options in a file
by Joost (Canon) on Jun 21, 2006 at 13:20 UTC
      Note that, with the config extract provided by OP,
      # CONFIG_HOTPLUG_PCI is not set
      doesn't match your split, which would prevent the value from being set if needed.
Re: editing options in a file
by ptum (Priest) on Jun 21, 2006 at 13:21 UTC

    So, what have you tried?

    Sounds like you want to open the file, read its contents into a hash, muck about with those contents, and write the contents back out. You might find that Tie::File will do the trick for you.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: editing options in a file
by dsheroh (Parson) on Jun 21, 2006 at 15:34 UTC
    Something like (untested, may not even compile):
    my %config_settings = ( CONFIG_NET => 'y', CONFIG_HOTPLUG => 'y', # whatever else you want to set ) open(KERNEL_CONFIG, '<', '/usr/src/linux2.4/.config'); open(NEW_CONFIG, '>', '/usr/src/linux2.4/.config.new'); while my $config_line (<KERNEL_CONFIG>) { $config_line =~ /([A-Z0-9_])/; $config_line = $1 . '=' . $config_settings{$1} . "\n" if exists $con +fig_settings{$1}; print NEW_CONFIG $config_line; } close KERNEL_CONFIG;
    This will (well, should...) create a new configuration in .config.new with the entries given in %config_settings changed to match the settings in the hash and all other lines left untouched. After verifying the results, you can then just rename .config.new to .config and you should be ready to rebuild the kernel.

    Update: Initially forgot to include the \n when constructing a new $config_line.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://556646]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-12-18 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (42 votes), past polls