而不是你尝试过的:
cat ${MYSQLDUMP} | \ # Output MYSQLDUMP File
其他人提到这应该可行:
cat ${MYSQLDUMP} | # Output MYSQLDUMP File
由于分割线并不总是以竖线 (|) 结尾,但是,您可以将 cmets 放在自己的行上,如下所示:
date && \
# List current directory
ls -l | awk '{ \
# Filename is in the ninth column
# This is just making "ls -l" work mostly like "ls -1"
print $9 }'
只是不要在字符串中间这样做:
echo " Hello \
# Localized name for your planet:
world."
在你的情况下,你可以使用这个方法:
cat ${MYSQLDUMP} | \
# Output MYSQLDUMP File
扩展示例:
# Create .csv file from MySQL dump file
cat ${MYSQLDUMP} |
# Output MYSQLDUMP File
# and pipe to first sed command
sed '1d' | \
# Pipe output to tr
tr ",;" "\n" | \
# Apply sed expression
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
# Apply another two sed expressions
# (and since whitespace is ignored, you can intent for clarity)
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
# Apply three more sed expressions
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
# Use tr to ...
tr "\n" "," | \
# Apply yet another two sed expressions
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \
# Apply the final three sed expressions
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
...或混合使用两种方法:
# Create .csv file from MySQL dump file
cat ${MYSQLDUMP} | # Output MYSQLDUMP File
# and pipe to first sed command
sed '1d' | \
# Pipe output to tr
...
(我相信这两种方法都有效,因为 shell 脚本文件是逐行解析的,CLI 输入也是如此。)
最后的笔记:
-
重要的是要记住,在使用时,续行字符 (\) 应该是该行中的最后一个字符 (即使是一个被遗忘的尾随空格也会毁了你的夜晚) .
-
如果从命令行手动输入,如果您打算使用命令历史记录功能,请仅使用第二种方法(每条注释单独一行)。
-
如果使用历史记录并希望保留 cmets,请不要使用这两种方法中的任何一种 - 使用该问题的不同答案中的一种。