jassim mishal
jassim mishal

Reputation: 83

finding and replacing path url using numbers by sed or awk

i have one big file contains over 200000 numbers not ordering i would like to add url directory 1/2/ to my links depend it on first two numbers ,, for example my file: all.mp3

19471.mp3
19472.mp3
28463.mp3
28464.mp3
35437.mp3
35435.mp3

expected to look like this

http://www.example.com/files/1/9/19471.mp3
http://www.example.com/files/1/9/19472.mp3
http://www.example.com/files/2/8/28463.mp3
http://www.example.com/files/2/8/28464.mp3
http://www.example.com/files/3/5/35437.mp3
http://www.example.com/files/3/5/35435.mp3

Upvotes: 0

Views: 129

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74645

You can use sed to do this:

$ sed -r 's~(.)(.)~http://www.example.com/files/\1/\2/&~' file
http://www.example.com/files/1/9/19471.mp3
http://www.example.com/files/1/9/19472.mp3
http://www.example.com/files/2/8/28463.mp3
http://www.example.com/files/2/8/28464.mp3
http://www.example.com/files/3/5/35437.mp3
http://www.example.com/files/3/5/35435.mp3

Capture the first two characters and use them in the replacement string (\1 and \2). & refers to the entire match.

You could make the match a little stricter by using [0-9] rather than . inside the capture groups if necessary. If your version of sed doesn't support extended regular expressions (the -r or -E switch), you have to escape the parentheses:

$ sed 's~\(.\)\(.\)~http://www.example.com/files/\1/\2/&~' file

Upvotes: 3

Related Questions