tags:

views:

155

answers:

4

How can I comment on each line of the following lines from a script:

   cat ${MYSQLDUMP} | \
   sed '1d' | \
   tr ",;" "\n" | \
   sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
   sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
   sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
   tr "\n" "," | \
   sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \
   sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}

If I try and add a comment say "cat ${MYSQLDUMP} | \ #Output MYSQLDUMP File"

I get "#Delete: not found" ? Is it possible to comment here or not because of " | \ "

A: 

This will have some overhead, but technically it does answer your question:

echo abc $(: put your comment here) \
     def $(: another chance for a comment) \
     xyz etc

Or:

echo abc `: put your comment here` \
     def `: another chance for a comment` \
     xyz etc

And for pipelines specifically, there is a clean solution with no overhead:

echo abc |         # normal comment OK here
     tr a-z A-Z |  # another normal comment OK here
     sort |        # the pipelines are automatically continued
     uniq          # final comment
DigitalRoss
Seems rather complex, if there no simpler method?
BassKozz
Ok, I've added a slightly simpler variation.
DigitalRoss
Can you modify your answer just to show the fact that the backslash is not needed so I can put the comments next to each line and just use a pipe?
BassKozz
Ok, good idea. Done.
DigitalRoss
A: 

The backslash escapes the #, interpreting it as its literal character instead of a comment character.

tobiasvl
+2  A: 

The trailing backslash must be the last character on the line for it to be interpreted as a continuation command. No comments or even whitespace are allowed after it.

You should be able to put comment lines in between your commands

# output MYSQLDUMP file
cat ${MYSQLDUMP} | \
# simplify the line
sed '/created_at/d' | \
# create some newlines
tr ",;" "\n" | \
# use some sed magic
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
# more magic
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
# even more magic
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
tr "\n" "," | \
# I hate phone numbers in my output
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \ 
# one more sed call and then send it to the CSV file
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
mobrule
The \ is not necessary when the pipeline command component ends with |
DigitalRoss
DigitalRoss,You are correct, I can just use the pipe and not the backslash and then my #comments will work perfectly... can you post that as an answer so I can accept it.
BassKozz
A: 

As DigitalRoss pointed out, the trailing backslash is not necessary when the line woud end in |. And you can put comments on a line following a |:

 cat ${MYSQLDUMP} |         # Output MYSQLDUMP file
 sed '1d' |                 # skip the top line
 tr ",;" "\n" | 
 sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' |
 sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' |
 sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' |
 tr "\n" "," |
 sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' |   # hate phone numbers
 sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
mobrule