我试图在bash文件中的几行上传播一个sed命令。我有多种模式可以检查,我希望通过换行来分离一些模式。这是我当前的脚本不起作用,将所有内容放在一行上但是我希望只是稍微清理一下并分开。
#!/bin/sh -f
#files=$(ls -1 | egrep '.avi|.mkv');
#echo $files
for f in *.mkv *.avi;
do
renF=`echo $f | tr '.' ' ' | sed -e 's/ \([^ ]*\)$/.\1/; s/\ \[sharethefiles\ com\]//i' \
-e 's/\ x264\-Ctu//i; s/\ x264\-Bia//i; s/\ x264\-Fov//i' \
-e 's/\ Xvid\-Lol//i; s/\ Xvid\-Xor//i; s/\ Xvid\-Notv//i; s/\ Xvid\-Fqm//i; s/\ Xvid\-p0w4//i; Xvid\-BIA//i; s/\ Xvid\-Chgrp//i; s/\ Xvid\-Fov//i' \
-e 's/\ PDTV\-Fov//i; s/\ PDTV\-River//i; s/\ PDTV\-Sfm' \
-e 's/\ Dts\-Chd//i; s/\ WEB\-DL//i; s/\ H264\-SURFER//i' \
-e 's/\ Dsr//; s/\ WS//i; s/\ PDTV//i; s/\ X264//i; s/\ Blu\-Ray//; s/\ Bdrip//i; s/\ Bluray//i; s/\ HDTV//i; s/\ DTS//i; s/\ AC3//i; s/\ Dd5//i; s/\ Dualaudio//i; s/\ AAC2//i; s/\ XVID//i'`
#echo $renF
if [ "$f" == "$renF" ]; then
echo "FileName already cleaned."
else
mv "$f" "$renF"
fi
done
感谢您的帮助!
答案 0 :(得分:9)
问题在于这一行:
-e 's/\ Xvid\-Lol//i; s/\ Xvid\-Xor//i; s/\ Xvid\-Notv//i; s/\ Xvid\-Fqm//i; s/\ Xvid\-p0w4//i; Xvid\-BIA//i; s/\ Xvid\-Chgrp//i; s/\ Xvid\-Fov//i' \
缺少s/
。它应该是:
-e 's/\ Xvid\-Lol//i; s/\ Xvid\-Xor//i; s/\ Xvid\-Notv//i; s/\ Xvid\-Fqm//i; s/\ Xvid\-p0w4//i; s/\ Xvid\-BIA//i; s/\ Xvid\-Chgrp//i; s/\ Xvid\-Fov//i' \
我包含反斜杠以保持一致性。
此行缺少//i
:
-e 's/\ PDTV\-Fov//i; s/\ PDTV\-River//i; s/\ PDTV\-Sfm
修正:
-e 's/\ PDTV\-Fov//i; s/\ PDTV\-River//i; s/\ PDTV\-Sfm//i
我得到的错误消息是导致错误来源的原因:
sed: -e expression #3, char 93: unknown command: `X'
和
sed: -e expression #4, char 51: unterminated `s' command
以下是关于如何使其更具可读性和可维护性的建议:
while read -r pattern
do
sedscript+="$pattern;"
done <<EOF
s/ \([^ ]*\)$/.\1/
s/\ \[sharethefiles\ com\]//i
s/\ x264\-Ctu//i
s/\ x264\-Bia//i
...
s/\ XVID//i
EOF
renF=$(echo "$f" | tr '.' ' ' | sed -e "$sedscript")
答案 1 :(得分:4)
不确定我是否理解你,但如果你想要多行sed表达式,你可以做这样的事情:
v=`echo 123|sed 's/1/2/
s/2/3/
s/3/4/'`; echo $v
按预期显示423
。
如果你想在一行上,那也是可能的:
v=`echo 123|sed 's/1/2/ ; s/2/3/ ; s/3/4/'`; echo $v
给出相同的结果。
对于我所看到的,我实际上建议你在多行中做它,只是将每个-e放在一个单独的行上,因为我觉得它更具可读性。类似的东西:
v=`echo 123|sed \
-e 's/1/2/' \
-e 's/2/3/' \
-e 's/3/4/'`
echo $v
查看此链接以获取更多信息:
<{1>} 下的
答案 2 :(得分:1)
如果GNU sed可用(不是内置于OSX中),可能更好的替代方法是使用heredoc:
sed -f - "$IN" > "$OUT" << SED_SCRIPT
s/a/1/g
s/test/full/g
SED_SCRIPT
来源:https://unix.stackexchange.com/questions/45591/using-a-here-doc-for-sed-and-a-file