LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   stupid question about splitting command on more lines and comment them... (https://www.linuxquestions.org/questions/programming-9/stupid-question-about-splitting-command-on-more-lines-and-comment-them-4175481577/)

masavini 10-21-2013 06:24 AM

stupid question about splitting command on more lines and comment them...
 
hi,
when i split a sed command on more lines, i'm not able to add comment to each line:
Code:

echo "bubu" | \
        # new line after a pipe is not a problem
        sed -e 's/b/c/' \
                # new line with no pipe does not work...
                -e 's/b/c/'

can someone tell me if it's possible to add comment lines between a "non piped" splitted command?

i know that the backslashes escape the return char, so the multiline command will be read as a one-liner... so it should not be possible...

if it's a stupid question (and i'm really afraid so), just sorry...

kabamaru 10-21-2013 06:59 AM

Code:

echo "bubu" |
  sed '# comment
      s/b/c/
      # comment
      s/u/a/'


55020 10-21-2013 07:01 AM

You can abuse the "back tick" command substitution, but this solution is probably more ugly and confusing than the original problem:

Code:

sed -e 's/1/a/' \
    -e 's/2/b/' `# this comment is hiding inside a command substitution` \
    -e 's/3/c/'


masavini 10-21-2013 07:24 AM

this is not that bad:
Code:

echo "bubu" | \
        sed -e 's/b/a/' \
                `# this comment is hiding inside a command substitution` \
            -e 's/a/b/' \
            -e 's/u/c/'

thanks!

masavini 10-21-2013 07:26 AM

Quote:

Originally Posted by kabamaru (Post 5049592)
Code:

echo "bubu" |
  sed '# comment
      s/b/c/
      # comment
      s/u/a/'


this is nice, but it becomes tricky if sed commands contain more '...

kabamaru 10-21-2013 07:56 AM

Quote:

Originally Posted by masavini (Post 5049604)
this is nice, but it becomes tricky if sed commands contain more '...

What you say also applies to sed -e '...'. If you have single quotes inside the sed command you can:

a) use double-quotes to enclose the sed command:
Code:

echo "'" | sed -e "s/'/single-quote/"
b) if your sed commands are complex (multiple commands, special characters etc.), it's more elegant to just put all these to a sed script, where you don't worry about quotes, comments etc. and just call your script with:
Code:

echo "bubu" | sed -f sedscr


All times are GMT -5. The time now is 05:54 AM.