Perl equivalent substitution

hi Geeks,
my input file contains data like =>

[Songspk.name] 53 - Deewana Kar Raha Hai.mp3
[Songspk.name] 54 - Hale Dil.mp3
[Songspk.name] 55 - Ishq Sufiyana.mp3
[Songspk.name] 56 - Abhi Kuch Dino Se.mp3
[Songspk.name] 57 - Pee Loon Hoto Ki Sargam.mp3

I had used sed command to remove the prefix from the file name like

sed 's/^\[.*- \(.*.mp3\)/\1/' file 

it gives me the perfect result. but now I want the same result by using the perl substitution.

I tried this command

perl -e 's/^\[.*- \(.*.mp3\)/\1/' file

but unfortunately it doesn't return anything and echo $? returns 0.

Kindly advise .. thanks in advance.

-Lohit.

Try:

perl -pe 's/^\[.*- \(.*.mp3\)/\1/' file
1 Like

I don't think you need to escape ( in a perl regex. But perl -e doesn't work that way, it expects a perl program, not a sed statement.

Your regex looks more complicated than it needs to be, though -- why match the entire name, and put back part of it, instead of just matching the part you want to delete? Then you don't need any of sed or perl's advanced features.

$ echo "[a b c d e f g] qwerty.mp3" | sed 's/^\[[^]]*\] *//'

qwerty.mp3

$
1 Like

Right. Brackets' escapes are unnecessary in Perl regexes:

perl -pe 's/^\[.*- (.*.mp3)/\1/' file
1 Like

thank you very much guys :slight_smile: