shell - Comment multiple lines in a file using sed -


i working on mac osx. writing shell script add prefix "//" log messages in file. wrote following sed script:

    sed -i '' "s|"log+*"|"//log"|g" filename 

the script working fine when log message has single line. if log has multiple lines fails. eg:

    log("hi          how          you"); 

the output comes out :

    //log("hi            how            you"); 

but, want output be:

    //log("hi     //     how     //     you"); 

since haven't used sed don't know how this. so, possible using sed. if yes how?

thanks

the simplest method use address ranges

sed "/^\s*log.*;$/ s|^|//|; /^\s*log/, /);$/ s|^|//|" input 

what does?

  • /^\s*log.*;$/ s|^|//| if line starts log , ends ; substitute start, ^ //

  • /\s*^log/, /);$/ s|^|//|"

    • /^log/, /);$/ address range. lines within range, substitution performed. range of lines start first regex match end match.

test

$ cat input log("hi          how          you");  not commented;   log ("test);  not commented;  $  sed "/^\s*log.*;$/ s|^|//|; /^\s*log/, /);$/ s|^|//|" input //log("hi //         how //         you");  not commented;   //log ("test);  not commented; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -