ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed -n "1h;1! H;$ {;g;s/$search/$replace/g;p;}" /boot/grub/test.lst |head -n 4
Notice the extra space at '1! H' and '$ {'. You also just need one enclosing pair of double-quotes (" ").
Yeah! The extra space before H and { works! Thanx
I am gonna to try it all in loop with an array.
Here it is:
Code:
search="one.*five"
content=("one two three four five" "bla bla bla bla" "na na na na")
n=0
sed -n "1h;1! H;$ {;g;s/$block/${content[$n]}/g;p;}" /boot/grub/test.lst | head -n 4
search="one.*five"
content=("one two three four five" "bla bla bla bla" "na na na na")
n=0
sed -n "1h;1! H;$ {;g;s/$block/${content[$n]}/g;p;}" /boot/grub/test.lst | head -n 4
Returns some error
If the error is
Code:
sed: -e expression #1, char 0: no previous regular expression
then this is because there is no variable named 'block'.
Change your code either to
Code:
search="one.*five"
content=("one two three four five" "bla bla bla bla" "na na na na")
n=0
sed -n "1h;1! H;$ {;g;s/$search/${content[$n]}/g;p;}" /boot/grub/test.lst | head -n 4
or
Code:
block="one.*five"
content=("one two three four five" "bla bla bla bla" "na na na na")
n=0
sed -n "1h;1! H;$ {;g;s/$block/${content[$n]}/g;p;}" /boot/grub/test.lst | head -n 4
sed: -e expression #1, char 0: no previous regular expression
then this is because there is no variable named 'block'.
Oh, feathery. I thought that the syntax is wrong. But the original code, which I try to make working, still doesn't work. So I will show you the final part.
Code:
while [[ $n -lt ${#content[@]} ]]; do
n=$((n+1));
original_block=$(echo ${original_content[$n]} | awk 'BEGIN { FS="\n"; RS="";} {b = gensub(/([\]*+/()])/, "\\\\&", "g"); print b }')
set -x
sed -n "1h;1! H;$ {;g;s/$original_block/${content[$n]}/g;p;}" /boot/grub/test.lst
done;
$original_content - is array with blocks of original code of menu.lst
$content - is array with blocks of code that was changed
$original_block - is one item (block) from $original_content[$n]
$content[$n] - block that was changed
This report I have:
Code:
+ sed -n '1h;1! H;$ {;g;s/title Sata Mandriva kernel \(hd0,2\)\/boot\/vmlinuz BOOT_IMAGE=linux root=(hd0,2) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788 initrd \(hd0,2\)\/boot\/initrd.img/title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img/g;p;}' /boot/grub/test.lst
sed: -e výraz č.*1, znak 258: neukončený příkaz „s“
That is 3 lines of search:
Code:
title Sata Mandriva
kernel \(hd0,2\)\/boot\/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd \(hd0,2\)\/boot\/initrd.img
And 3 lines of replacement:
Code:
title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=(hd0,2) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img/g;p;}' /boot/grub/test.lst
I have changed pattern for search only. Drom replacement do I need to escape all chars like * / \ (), or only the slashes?
That depends. If you don't use extended regular expressions you don't have to escape the braces when they are supposed to be part of the regular expression. You don't even have to escape the slash '/' if you choose another delimiter in the substitution command, e.g.
Other words: I found that Search part and Replacement part must not have new lines characters. Because of the delimiter for was a slash: // and the search part contained slashes, there was error. Then I replaced this delimiter to ampersand @@, and sed works now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.