gpt4 book ai didi

bash - 将 'sed' 命令分布到多行

转载 作者:行者123 更新时间:2023-11-29 08:51:27 24 4
gpt4 key购买 nike

我正在尝试将一个 sed 命令分布到 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

感谢您的帮助!

最佳答案

问题出在这一行:

-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")

关于bash - 将 'sed' 命令分布到多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com