$ awk -v i=1 '/ \.mp3/ {sub(/ \.mp3/," "i++".mp3",$0)};1' input.txt
line 1
wget 1.mp3 url
line 2
wget 2.mp3
If the numerals in the wget URL are meant to be zero-padded, you can use awk's sprintf() to format i with leading zeroes. For example:
$ awk -v i=1 '/ \.mp3/ {sub(/ \.mp3/," "sprintf("%03i",i++)".mp3",$0)};1' input.txt
line 1
wget 001.mp3 url
line 2
wget 002.mp3
You could also combine sprintf() with wget's -O (--output-document) option to save the downloaded files with zero-padded filenames, so they sort correctly (e.g. 01,02,03,...10 instead of 1,10,2,3,...)
$ awk -v i=1 '/ \.mp3/ {sub(/ \.mp3/," "i".mp3 -O "sprintf("%03i",i++)".mp3",$0)};1' input.txt
line 1
wget 1.mp3 -O 001.mp3 url
line 2
wget 2.mp3 -O 002.mp3
All of the above one-liners will output to stdout. To overwrite the original, redirect to a new file and either mv it or overwrite the original.
awk -v i=1 '/ \.mp3/ {sub(/ \.mp3/," "i++".mp3",$0)};1' orig.txt > new.txt
and then either:
mv new.txt orig.txt
This will replace orig.txt with a new file created with the permissions specified by the current umask value. It may also have a new owner and/or group (depending on who runs it, and what their default group is). It will also have a new inode number, breaking any hard-links that might exist.
Most programs that have an "in-place edit" option (e.g. sed -i or perl -i) do this. In most cases, this is fine because it's being run by the same user with the same umask as the original file's owner, and few files have multiple hard links.
or
cat new.txt > orig.txt
rm new.txt
This will overwrite the contents of orig.txt with the contents of new.txt, and then delete new.txt. orig.txt will have the same inode, owner, group, and permissions as before.
netiquette multiposting