'I need another set of eyes to tell me what's wrong with this pipe and sed
Again I am trying to rename some video files, and again I am having problems with my command. If I could get another set of eyes to look at this and figure out what i'm not seeing I would be grateful.
I have files like this;
'DARK Matter S01E03 Episode Three.mp4'
'DARK Matter S01E04 Episode Four.mp4'
etc...
I am trying to remove the " Episode Three", " Episode Four" etc from the names and I wrote this command, but it's not quite working as expected.
for file in *Episode*;do echo "rename \"$file\" "\"$file" | sed 's/ Episode*./.')";done
The result of this command is;
rename "DARK Matter S01E03 Episode Three.mp4" "DARK Matter S01E03 Episode Three.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E04 Episode Four.mp4" "DARK Matter S01E04 Episode Four.mp4 | sed 's/ Episode*./.')
rename "DARK Matter S01E05 Episode Five.mp4" "DARK Matter S01E05 Episode Five.mp4 | sed 's/ Episode*./.')
It's not recognizing the pipe to sed and my sed command itself is not written correctly (I know, i'm terrible with regular expressions). I would appreciate any help you could give.
Solution 1:[1]
I figured it out myself finally;
for file in *Episode*;do echo "rename \"$file\" \"$file\"" | sed 's/ Episode[^.]*//2';done
rename "DARK Matter S01E03 Episode Three.mp4" "DARK Matter S01E03.mp4"
rename "DARK Matter S01E04 Episode Four.mp4" "DARK Matter S01E04.mp4"
rename "DARK Matter S01E05 Episode Five.mp4" "DARK Matter S01E05.mp4"
Solution 2:[2]
Using a regex and pulling the matching pieces from the BASH_REMATCH[] array:
regex='(.*) Episode [^.]+(.*)'
for file in *Episode*
do
if [[ "${file}" =~ $regex ]]
then
# typeset -p BASH_REMATCH
newfile="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
cp "${file}" "${newfile}"
fi
done
NOTES:
- the
regexis defined to pickup anything after the first period (eg,.mp4,.mkv,some.other.suffix) - uncomment the
typeset -pcommand to display the contents of theBASH_REMATCH[]array - replace
cpwithmvonce the results have been verified
Assuming the following files:
$ ls -l *Dark*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv
The code results in the following:
$ ls -l DARK*
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E03\ Episode\ Three.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E03.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:02 DARK\ Matter\ S01E04\ Episode\ Four.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E04.mp4
-rw-rw----+ 1 username None 0 Feb 23 20:03 DARK\ Matter\ S01E05\ Episode\ Five.mkv
-rw-rw----+ 1 username None 0 Feb 23 20:08 DARK\ Matter\ S01E05.mkv
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Radamand |
| Solution 2 | markp-fuso |
