LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   insert text to a file using command line (https://www.linuxquestions.org/questions/linux-newbie-8/insert-text-to-a-file-using-command-line-785456/)

replica88 01-28-2010 04:34 PM

insert text to a file using command line
 
I need to be able to edit a file from the commend line as I intend to script this operation, the file is called menu.lst

The original is as below

Code:

title                Ubuntu 8.04.3 LTS, kernel 2.6.24-24-generic
root                (hd0,0)
kernel                /boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro quiet splash
initrd                /boot/initrd.img-2.6.24-24-generic
quiet

title                Ubuntu 8.04.3 LTS, kernel 2.6.24-24-generic (recovery mode)
root                (hd0,0)
kernel                /boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro single
initrd                /boot/initrd.img-2.6.24-24-generic

title                Ubuntu 8.04.3 LTS, memtest86+
root                (hd0,0)
kernel                /boot/memtest86+.bin
quiet

I need to add acpi=force to two lines as below

Code:

title                Ubuntu 8.04.3 LTS, kernel 2.6.24-24-generic
root                (hd0,0)
kernel                /boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro quiet splash acpi=force
initrd                /boot/initrd.img-2.6.24-24-generic
quiet

title                Ubuntu 8.04.3 LTS, kernel 2.6.24-24-generic (recovery mode)
root                (hd0,0)
kernel                /boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro single acpi=force
initrd                /boot/initrd.img-2.6.24-24-generic

title                Ubuntu 8.04.3 LTS, memtest86+
root                (hd0,0)
kernel                /boot/memtest86+.bin
quiet

Any ideas?

pixellany 01-28-2010 04:45 PM

Code:

sed -i '/^kernel/s/$/ acpi=force/' menu.lst
Test it first without the "-i" flag

colucix 01-28-2010 04:49 PM

The most straightforward method to edit a file using a command is by means of the sed editor. If you're already familiar with it, take in mind you can edit the file in place using the option -i. But always do a backup copy of your original file, until you're sure the result is exactly as your expectations. sed permits to do this automatically, just by adding a suffix to the option -i, e.g.
Code:

sed -i.bck <sed command here> file
will create a backup copy named file.bck, before editing file. Maybe you already know all of this, but it's worth to mention for safety.

Regarding your question, using sed you can just address the lines containing the keyword "kernel" at the beginning and add " acpi=force" at the end. For example:
Code:

sed '/^kernel/ s/$/ apci=force/' menu.lst
Don't forget the space before "apci".

Edit: too late... sorry for redundancy. Pixellany, I envy your ability to be clear and succinct... :)

Tinkster 01-28-2010 05:25 PM

Slight modification
Code:

sed -i '/^kernel                \/boot\/vmlinuz/s/$/ acpi=force/' menu.lst

Because i don't know whether memtest will handle the acpi=force

colucix 01-28-2010 05:50 PM

Quote:

Originally Posted by Tinkster (Post 3844221)
Because i don't know whether memtest will handle the acpi=force

He he.. that's right! :)


All times are GMT -5. The time now is 04:09 PM.