0

Im trying to rename some files and need help with formatting a proper 'sed' command;

I have a file of movie episodes that looks like this;

SG1_s10e8"Memento Mori"
SG1_s10e9"Company of Thieves"
SG1_s10e10"The Quest"
SG1_s10e11"The Quest Part 2"

and I need to pad the episode # with a '0' like this;

SG1_s10e08"Memento Mori"
SG1_s10e09"Company of Thieves"
SG1_s10e10"The Quest"
SG1_s10e11"The Quest Part 2"

I'm sure sed is the correct tool, im just not real keen with the command syntax.

I would appreciate any help.

Radamand
  • 175
  • 1
  • 12

2 Answers2

3
sed 's/e\([0-9]"[^"]*"\)/e0\1/' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

you could try this:

sed 's/e\([0-9]\)"/e0\1"/'
  • Not bad but it'd fail for `SG1_s10e08"Memento More9"` for example. – Ed Morton Feb 10 '17 at 15:49
  • No, because it only replaces the first occurrence; that would happen with the g at the end, as in 's///g', but not with 's//'. Right? – João Miranda Feb 10 '17 at 15:53
  • I had the wrong example at first, edited my comment now with the correct example. sorry. What I'm saying is if `e[0-9]"` does NOT occur in the expected position before the start of the double quoted string but then does occur at the end of the string then the replacement would happen at that wrong location. – Ed Morton Feb 10 '17 at 15:53
  • 1
    True. You'd have to add something more, like you did. – João Miranda Feb 10 '17 at 15:57