Reputation: 111
I have a folder with ebooks in epub format and their names are in the shape "title_of_the_book_-_name_of_the_author". I have to swap the two parts of the names of the ebooks so they become "name_of_the_author-title_of_the_book". I have written this command that works in almost every situations:
ls *.epub | sed 's/^\([^-]*\)-_\([^.]*\)/\2-\1/' | sed -E 's/.(.epub)$/\1/g'
I'm going to explain the code. First I list all the epub files in the folder, then I match the title of the book and the author of the book and I swap them and finally I delete the _ that goes at the end of the title of the book. This works in almost every situations, but when the title of the book contains a - this doesn't work. How can I match the substring _- instead of just this character - ? I think that would solve my problem.
Upvotes: 1
Views: 138
Reputation: 18687
If I understand your problem/question correctly, this sed
expression should do the trick:
sed -E 's/^(.*)_-_(.*)\.epub$/\2-\1.epub/'
This works under the assumption that every title is separated from author with a fixed 3-character string _-_
. We use greedy capturing groups (.*)
, but anchor the beginning and end of the pattern, so we capture the title and author correctly.
For example:
$ echo "title_of_the_book_-_name_of_the_author.epub" | sed -E 's/^(.*)_-_(.*)\.epub/\2-\1.epub$/'
name_of_the_author-title_of_the_book.epub
$ echo "semi-title_of_the_book_-_name_of_the_author.epub" | sed -E 's/^(.*)_-_(.*)\.epub/\2-\1.epub$/'
name_of_the_author-semi-title_of_the_book.epub
Upvotes: 2