linux - SED replacing with 'possible' newline -


i have sed command working fine, except when comes across newline right in file somewhere. here command:

sed -i 's,<a href="\(.*\)">\(.*\)</a>,\2 - \1,g' 

now, works perfectly, ran across file has a tag so:

<a href="link">click         here now</a> 

of course didn't find one. need modify somehow allow lines breaks in search. have no clue how make allow unless go on entire file first off , remove \n before hand. problem there loose formatting in file.

you can inserting loop sed script:

sed -e '/<a href/{;:next;/<\/a>/!{n;b next;};s,<a href="\(.*\)">\(.*\)</a>,\2 - \1,g;}' yourfile 

as-is, leave embedded newline in output, , wasn't clear if wanted way or not. if not, substitute out newline:

sed -e '/<a href/{;:next;/<\/a>/!{n;b next;};s/\n//g;s,<a href="\(.*\)">\(.*\)</a>,\2 - \1,g;}' yourfile 

and maybe clean spaces:

sed -e '/<a href/{;:next;/<\/a>/!{n;b next;};s/\n//g;s/\s\{2,\}/ /g;s,<a href="\(.*\)">\(.*\)</a>,\2 - \1,g;}' yourfile 

explanation: /<a href/{...} lets ignore lines don't care about. once find 1 like, check see if has end marker. if not (/<\a>/!) grab next line , newline (n) , branch (b) :next see if we've found yet. once find continue on substitutions.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -