bash 将“sed”命令扩展到多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4198233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:55:47  来源:igfitidea点击:

Spread 'sed' command over multiple lines

bashsed

提问by Ratty

I am trying to spread one sed command over several lines in a bash file. I have multiple patterns that sed checks for and I am hoping to separate some of the patterns by a line change. Here is my current script that does not work, putting everything on one line works but I was hoping to just clean it up a bit and split things up.

我试图在 bash 文件中的多行中传播一个 sed 命令。我有多个 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/ \([^ ]*\)$/./; 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

Thanks for any help!

谢谢你的帮助!

采纳答案by Paused until further notice.

The problem is on this line:

问题出在这一行:

-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' \

There's a missing s/. It should be:

有一个失踪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 included the backslash for consistency.

为了一致性,我包含了反斜杠。

This line is missing //i:

缺少这一行//i

-e 's/\ PDTV\-Fov//i; s/\ PDTV\-River//i; s/\ PDTV\-Sfm

Corrected:

更正:

-e 's/\ PDTV\-Fov//i; s/\ PDTV\-River//i; s/\ PDTV\-Sfm//i

The error messages I got were what led me to the source of the errors:

我得到的错误消息使我找到了错误的根源:

sed: -e expression #3, char 93: unknown command: `X'

and

sed: -e expression #4, char 51: unterminated `s' command

Here's a suggestion on how to make this more readable and maintainable:

以下是有关如何使其更具可读性和可维护性的建议:

while read -r pattern
do
    sedscript+="$pattern;"
done <<EOF
s/ \([^ ]*\)$/./
s/\ \[sharethefiles\ com\]//i
s/\ x264\-Ctu//i
s/\ x264\-Bia//i
...
s/\ XVID//i
EOF

renF=$(echo "$f" | tr '.' ' ' | sed -e "$sedscript")

回答by icyrock.com

Not sure if I understood you, but you can do something like this if you just want to have multiple lines of sed expressions:

不确定我是否理解你,但如果你只想拥有多行 sed 表达式,你可以做这样的事情:

v=`echo 123|sed 's/1/2/
s/2/3/
s/3/4/'`; echo $v

This displays 423, as expected.

423如预期的那样显示。

If you want it on one line, that's possible, too:

如果你想在一行上,那也是可能的:

v=`echo 123|sed 's/1/2/ ; s/2/3/ ; s/3/4/'`; echo $v

gives the same result.

给出相同的结果。

For what I see, I would actually suggest doing it in multiple lines as you are already doing, just putting every -e on a separate line, as I think that is more readable. Something like:

对于我所看到的,我实际上建议像您已经在做的那样在多行中进行,只需将每个 -e 放在单独的行上,因为我认为这更具可读性。就像是:

v=`echo 123|sed \
   -e 's/1/2/' \
   -e 's/2/3/' \
   -e 's/3/4/'`
echo $v

Check this link for more information:

查看此链接以获取更多信息:

under Quoting multiple sed lines in the Bourne shell

在下面 Quoting multiple sed lines in the Bourne shell

回答by mahemoff

A possibly nicer alternative would be to use a heredoc, if GNU sed is available (not built into OSX):

如果 GNU sed 可用(未内置到 OSX 中),则可能更好的替代方法是使用 heredoc:

sed -f - "$IN" > "$OUT" << SED_SCRIPT
  s/a/1/g
  s/test/full/g
SED_SCRIPT

Source: https://unix.stackexchange.com/questions/45591/using-a-here-doc-for-sed-and-a-file

资料来源:https: //unix.stackexchange.com/questions/45591/using-a-here-doc-for-sed-and-a-file