Spread 'sed' command over multiple lines

2020-08-21 02:23发布

问题:

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.

#!/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

Thanks for any help!

回答1:

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:

-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:

-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/ \([^ ]*\)$/.\1/
s/\ \[sharethefiles\ com\]//i
s/\ x264\-Ctu//i
s/\ x264\-Bia//i
...
s/\ XVID//i
EOF

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


回答2:

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

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

This displays 423, as expected.

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:

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

Check this link for more information:

  • http://www.grymoire.com/Unix/Sed.html

under Quoting multiple sed lines in the Bourne shell



回答3:

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

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



标签: bash sed