| |
| |
| |
|
Page: 1 2 3 4
Comments:
<0> Hi <1> hey folks <0> is it possible to replace a string in a file without losing the tabulation ? <1> i'm trying to apply a block filter, but I only need to apply to the first block it matches, ignoring the rest that could match <2> Lutin: yes <1> Lutin: you can limit the regex using \t <1> is that possble ? <0> csmanx, ok <2> csmanx: Yes. <2> csmanx: A less elegant method is to simply print the rest of the input in a loop: :a n ba <2> sed -e '/foo/,/bar/{ s/xxx/yyy/g; /bar/{ :a' -e 'n;ba' -e '}' -e '}' <1> i don't get it after the second /bar/ =/ <2> It's a loop. : is a label, n prints and reads, b jumps to the label. <1> right <2> So, when it finds the end of the first block, it simply reads and prints all of the remaining lines. <1> too many '
<1> prec: you mean, when it finishes processing the first block <2> No, there's just enough ' <2> ;) <2> csmanx: Right. <1> is -e acting recursively ? <2> No, sed takes all the -e options and concatenates them into a single script which it then executes. <2> printf '/foo/,/bar/{\n\ts/xxx/yyy/g\n\t/bar/{\n\t\t:a\n\t\tn\n\t\tba\n\t}\n}\n' >csmanx.sed <2> sed -f csmanx.sed <1> =) <1> prec: i'm intrigued about the -e decoration <1> what is the second one for in the :a loop? <2> ? <2> All the -e options are concatenated into a single script. <2> The : and b commands are terminated by the end of the line, hence you cannot (portably) use: sed -e ':a;n;ba' <1> prec: right , but they seem not be executing anything ' -e ' ? <2> What's your script look like? <1> prec: not refering to my script, but to the example here <1> =) <2> It works for me. Are you using an sh-compatible shell? <1> so :a starts the loop , but '-e' does what? <2> -e doesn't do anythinig <2> -e is not part of the script <2> -e is how you specify the script. <2> In my example, there are 8 command line arguments to sed. <1> i thought it meant "execute this" <2> They consist of four -e script pairs. <1> i see <2> All four option arguments are concatenated into a single script _before_ sed starts its execution phase. <2> sed -e 's/1/2/g' -e 's/3/4/g' <2> sed -e 's/1/2/g;s/3/4/g' <1> that one i know <2> sed 's/1/2/g;s/3/4/g' <1> right <2> Those are all the same. <1> but going back to the question, sed doesn't have an explicit n blocks to process switch, right? <2> Nope. <2> By "block", you mean some address range, right? <1> yeah <2> OK. <1> if i have <!-- <thing_to_enable ... /> --> <1> like five times <1> and I just want to remove the comments from the first block because I only need 1 <1> btw, removing --> just one time doesn't work =( <2> ? <1> it removes all of them <1> from all matching blocks <2> What's your script look like? <1> sed -e '/<element_to_enable/,/-->/{s/-->//g;/-->/{:a' -e 'n;ba' -e '}' -e '}' <2> OK, the problem is that the /-->/ is never matched because it occurs on the same line as the start address. <1> it is matched and removed from all blocks, i don't know about the loop part <2> OK, it is matched by the s command, yes. <2> But it is never matched by the second address in your address range. <1> right <1> what would be the start address there? <1> the one that is gone? <1> right <1> =) <2> seq 19 22 |sed -e '/2/,/0/!d' <2> vs.: seq 19 22 |sed -e '/0/,/1/!d' <2> In other words, the block does not end until the _next_ line which matches the end address. <2> brb
<1> the next line that matches, not the next match in the same line <2> csmanx: Exactly. <2> However, we can fix your script. <2> /blah/,/-->/{/-->/{s///;:a n ba } } <2> sed -e '/blah/,/-->/{/-->/{s///;:a' -e'n;ba' -e \} -e \} <1> hmmm <2> Perhaps I should test that ... <1> if --> matches do the matching again and if it matches replace with nothing and print the rest ? <1> lemme try <1> it worked <1> but i got the stuff printed twice <2> Yeah, it works here. <2> What stuff? <1> the blocks <1> echo '<!-- <element_to_enable /> --> <!-- <element_to_enable /> -->' | sed -e '/<element/,/-->/{/-->/{s///;:a' -e'n;ba' -e \} -e \} <2> They don't print twice for me. <1> huh? <1> geez... <1> heh, my devel box is broken then <1> GNU sed version 4.0.7 <2> I've got 4.1.4 for some reason. <1> now you freaked me out with the \} and the not-really-unmatched ' =P <2> But it is pretty standard sed. <1> hmmm <2> Hmm? I've got two pairs of '' in my last example. <1> yeah, i'm just freaking out with { syntax <2> I get this output: "<!-- <element_to_enable /> <!-- <element_to_enable /> -->" <1> right <1> i get the same on my mandriva laptop <1> but not on the rhel 3 <3> how would I remove the abc from this string? "<P***word>abc</P***word>" <2> s/abc// <4> heh <3> heh <2> <input xml2pyx |sed '/^(P***word/,/^)P***word/d' |pyx2xml >output <2> <input xml2pyx |sed '/^(P***word/,/^)P***word/{/^-/d;}' |pyx2xml >output ## this leaves the element, but removes any CDATA content. <3> I don't have xml2pyx and can't install it. I'd also like to be able to change abc to things other than blank. <2> cekc: You will need to use an XML parser (easy) or write an XML parser in sed (very difficult). <2> I have a version of "xml2pyx" written in sed. <2> However, I don't have pyx2xml yet, so ... ;) <3> why must I use an XML parser? Is it the < and > characters? <2> Because you are parsing XML, yes? <2> By definition, you need to use an XML parser to do that. <3> I am sanitizing p***words from an XML file, and the p***words will always be between the strings <P***word> and </P***word> <3> I'd be just as happy removing BBB from AAABBBCCC where BBB is variable and AAA and CCC are known <2> [^<] might come in handy then. <3> I have to take off now, might be back on later to continue figuring it out. Thank you very much for the help <5> hi there <5> anyone up? <5> :) <1> hello again =) <6> i'm trying to match lines which contain either TE or TA: echo TAST | sed -ne '/(te|ta)/I p' <6> but the (te|ta) does not seem to work <6> any hint? <7> veal: echo TAST | sed -ne '/te\|ta/I p' <6> thank you! <7> veal http://www.student.northpark.edu/pemente/sed/sed1line.txt AND sed.sf.net <1> cl***ic mistake <1> my just-two-line matcher doesn't work =( <1> a simple s/foo\nbar/boo/ won't work <1> =( <7> csmanx: edit between regexs: sed '1,/foo/!{ /bar/,/foo/!s/^/added text /; }' filename <1> gnubien: what is the 1 for ? <7> csmanx: try it without the 1 to see the diff <1> ok <8> wouldn't it be easier to use sed '/foo/{N;s/foo\nbar/boo/}' file <8> ? <7> helloman: nice <1> helloman: exactly <1> helloman: i just go that <1> by myself ^_^ <1> but now, i just want to replace the foo <8> when there is bar on the next line? <1> hmmm the boo should work
Return to
#sed or Go to some related
logs:
mkpart =-1s #openzaurus nachplus MAXVOLRAT exception in __init__ python ubuntu no sda1 wrong current_timestamp mysql #css suse kdrc default slashcot
|
|