sed - testing multiline search and replace
Hi, I found a example how to do multiline search and replace. I try to make this working to my needs but with no success :-(
Step 1 I make a file with few lines: one two five Now I want to change it by the code: Code:
sed -n '1h;1!H;${;g;s/one*five/one two three four five/g;p;}' test.lst one two five no change. Somebody help? Original code: Code:
sed -n '1h;1!H;${;g;s/<h2.*</h2>/No title here/g;p;}' sample.php > sample-edited.php; |
Quote:
I tried your code with one small change: Code:
sed -n '1h;1!H;${;g;s/one.*five/one two three four five/g;p;}' test.lst Hope this helps. |
Quote:
Step 2 Code:
clear How should I to correct it to have regular syntax? |
Quote:
Code:
sed -n "1h;1! H;$ {;g;s/$search/$replace/g;p;}" /boot/grub/test.lst |head -n 4 |
Quote:
I am gonna to try it all in loop with an array. Here it is: Code:
search="one.*five" |
Quote:
Code:
sed: -e expression #1, char 0: no previous regular expression Change your code either to Code:
search="one.*five" Code:
block="one.*five" |
Quote:
Code:
while [[ $n -lt ${#content[@]} ]]; do $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 Code:
title Sata Mandriva Code:
title Sata Mandriva Code:
sed: -e expression #1, char 258: "neukončený příkaz"(not enclosed command) „s“ |
Quote:
|
Quote:
|
Quote:
Code:
sed -e 's@expression1@expression2@' file |
Code:
sed -n '1h;1! H;$ {;g;s@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@title Sata Mandriva after "@title Sata Mandriva" and after "splash=silent vga=788" |
So I found I have to escape all New line characters (as I call breakline).
I used this code to escape some characters and breaklines on Search part and Replacement part: Code:
echo "${content[1]}" | awk '{b = gensub(/([\]*+/])/, "\\\\&", "g"); printf "%s\\n",b }' |
All times are GMT -5. The time now is 06:38 PM. |