Skip to main content
deleted 120 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{    s|/\*|\n&|              #if ! */ ...
       repl 1st \|/\*|!{             #and if* !w/ \n/* ...
     h;      s|foo|bar|g;/\n/!b  s|foo|bar|g;b    #repl#hold; repl all foo w/ bar, branch out...
    };  s||\n|;h             bar; #elseifif ! */ and /* repl 1st /* w/ \n;\n savebranch
        s|foo|bar|g;G         #replG; all foo w/ bar; Get save
 s|\n.*\n||;:n       s|\n.*\n|/*|;:n;n    #Get; #removeclear difference; :new label;label
 new line
   n;      \|*/|!bn;s|^|\n/*|    #if ! new#new lineline; if ! */; branch new label
     };s|*/|\n&|g                    #repl all */ w/ \n*/
       s|foo|&\nbar|g;:r             #repl all foo w/ foo\nbar
       s|\(/\*[^\n]*\)\nbar|\1|g;tr  #repl all /*[^\n]*\nbar w/ foo
       s|foo\n\(b\)|\1|g             #repl all foo\nbar w/ bar
       s|^\n/.||;s|\n||g             #clear any \n inserts
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{                  #if ! */ ...
         \|/\*|!{             #and if ! /* ...
             s|foo|bar|g;b    #repl all foo w/ bar, branch out...
    };  s||\n|;h              #elseif ! */ and /* repl 1st /* w/ \n; save
        s|foo|bar|g;G         #repl all foo w/ bar; Get save
        s|\n.*\n|/*|;:n;n     #remove difference; :new label; new line
        \|*/|!bn;s|^|\n/*|    #if ! new line */; branch new label
};s|*/|\n&|g                  #repl all */ w/ \n*/
s|foo|&\nbar|g;:r             #repl all foo w/ foo\nbar
s|\(/\*[^\n]*\)\nbar|\1|g;tr  #repl all /*[^\n]*\nbar w/ foo
s|foo\n\(b\)|\1|g             #repl all foo\nbar w/ bar
s|^\n/.||;s|\n||g             #clear any \n inserts
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{ s|/\*|\n&|              #if ! */ repl 1st /* w/ \n/*
     h;      s|foo|bar|g;/\n/!b      #hold; repl all foo/bar; if ! \n branch
     G;      s|\n.*\n||;:n           #Get; clear difference; :new label
     n;      \|*/|!bn;s|^|\n/*|      #new line; if ! */ branch new label
     };s|*/|\n&|g                    #repl all */ w/ \n*/
       s|foo|&\nbar|g;:r             #repl all foo w/ foo\nbar
       s|\(/\*[^\n]*\)\nbar|\1|g;tr  #repl all /*[^\n]*\nbar w/ foo
       s|foo\n\(b\)|\1|g             #repl all foo\nbar w/ bar
       s|^\n/.||;s|\n||g             #clear any \n inserts
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello
deleted 2 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{                  #if ! */ ...
         \|/\*|!{             #and if ! /* ...
             s|foo|bar|g;b    #repl all foo w/ bar, branch out...
    };  s||\n|;h              #elseif ! */ and /* repl 1st /* w/ \n; save
        s|foo|bar|g;G         #repl all foo w/ bar; Get save
        s|\n.*\n|/*|;:n;n     #remove difference; :new label; new line
        \|*/|!bn;s|^|\n/*|    #if ! new line */; branch new label
};s|*/|\n&|g                  #repl all */ w/ \n*/
s|foo|&\nbar|g;:r             #repl all foo w/ foo\nbar
s|\(/\*[^\n]*\)\nbar|\1|g;tr  #repl all /*[^\n]*\nbar w/ foo
s|foo\n\(b\)|\1|g             #repl all foo\nbar w/ bar
s|^\n/.||;s|\n||g             #clear any \n inserts
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{ 
         \|/\*|!{ 
             s|foo|bar|g;b
    };  s||\n|;h
        s|foo|bar|g;G
        s|\n.*\n|/*|;:n;n
        \|*/|!bn;s|^|\n/*|
};s|*/|\n&|g
s|foo|&\nbar|g;:r
s|\(/\*[^\n]*\)\nbar|\1|g;tr
s|foo\n\(b\)|\1|g
s|^\n/.||;s|\n||g
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{                  #if ! */ ...
         \|/\*|!{             #and if ! /* ...
             s|foo|bar|g;b    #repl all foo w/ bar, branch out...
    };  s||\n|;h              #elseif ! */ and /* repl 1st /* w/ \n; save
        s|foo|bar|g;G         #repl all foo w/ bar; Get save
        s|\n.*\n|/*|;:n;n     #remove difference; :new label; new line
        \|*/|!bn;s|^|\n/*|    #if ! new line */; branch new label
};s|*/|\n&|g                  #repl all */ w/ \n*/
s|foo|&\nbar|g;:r             #repl all foo w/ foo\nbar
s|\(/\*[^\n]*\)\nbar|\1|g;tr  #repl all /*[^\n]*\nbar w/ foo
s|foo\n\(b\)|\1|g             #repl all foo\nbar w/ bar
s|^\n/.||;s|\n||g             #clear any \n inserts
'    <<\INPUT
asfoo   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello
deleted 2 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|^\n|,\|*'\|*/|!{
    
         \|*\|/|\*|!{ s|...||p;:n;N
                    s|\n|&&/*|;Ds|foo|bar|g;b
     };};\|*/|!{
            \|/\*|{ s||\n|;h
                    s|foo|bar|g;G
                    s|\n.*\n|/*|p;bn*|;:n;n
    };      s|foo|bar|g;b\|*/|!bn;s|^|\n/*|
    };s||\n&|g;s|*/|\n&|g
      s|foo|&\nbar|g;:r
      s|\(/\*[^\n]*\)\nbar|\1|g;tr
      s|foo\n\(b\)|\1|g
      s|^\n/.||;s|\n||g
'    <<\INPUT
asfoo   /* asdfooasdfoo 


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo 


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|^\n|,\|*/|{
            \|*/|!{ s|...||p;:n;N
                    s|\n|&&/*|;D
     };};\|*/|!{
            \|/\*|{ s||\n|;h
                    s|foo|bar|g;G
                    s|\n.*\n|/*|p;bn
    };      s|foo|bar|g;b
    };s||\n&|g
      s|foo|&\nbar|g;:r
      s|\(/\*[^\n]*\)\nbar|\1|g;tr
      s|foo\n\(b\)|\1|g
      s|^\n/.||;s|\n||g
'    <<\INPUT
asfoo   /* asdfooasdfoo
asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
INPUT

###OUTPUT

asbar   /* asdfooasdfoo
asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy.

sed '\|*/|!{  
         \|/\*|!{ 
             s|foo|bar|g;b
    };  s||\n|;h
        s|foo|bar|g;G
        s|\n.*\n|/*|;:n;n
        \|*/|!bn;s|^|\n/*|
};s|*/|\n&|g
s|foo|&\nbar|g;:r
s|\(/\*[^\n]*\)\nbar|\1|g;tr
s|foo\n\(b\)|\1|g
s|^\n/.||;s|\n||g
'    <<\INPUT
asfoo   /* asdfooasdfoo 


asdfasdfoo
asdfasdfoo
foo */foo /*foo*/ foo
/*.
foo*/
foo
hello

INPUT

###OUTPUT

asbar   /* asdfooasdfoo 


asdfasdfoo
asdfasdfoo
foo */bar /*foo*/ bar
/*.
foo*/
bar
hello
deleted 40 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
Loading
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
Loading