kirill
kirill

Reputation: 211

replace pattern in text file

Below my sample html file:

some text here <img src="http://site.com/7b399e20/77165/5fa/2a31ffb8.jpg"/> sometext here

some text here <img src="http://site.com/7b399e20/2a31ffb8.jpg"/> sometext here

some text here <img src="http://site.com/7b399e20/2a31ffb8.png"/> sometext here

some text here <img src="http://site.com/2a31ffb8.jpeg"/> sometext here

how do I make such a transformation:

some text here <img src="web/2a31ffb8.jpg"/> sometext here

some text here <img src="web/2a31ffb8.jpg"/> sometext here

some text here <img src="web/2a31ffb8.png"/> sometext here

some text here <img src="web/2a31ffb8.jpeg"/> sometext here

Thanks

Upvotes: 2

Views: 1817

Answers (3)

Colin Fine
Colin Fine

Reputation: 3364

I'll use Perl, because I know the syntax without having to look it up, but it would be very similar in awk or sed, as tekknolagi says:

perl -pi -e 's|http://site.com/.*([^/]+)"/>|web/$1"/>|;'  <filename>

This will preserve everything between the last / and the "

Upvotes: 3

hzm
hzm

Reputation: 430

What about using perl script? I have put your sample text into file foo.txt and here is the result:

$ cat foo.txt | perl -pe 's#http://.*/([a-z0-9A-Z]*\.)#web/\1#'
some text here <img src="web/2a31ffb8.jpg"/> sometext here
some text here <img src="web/2a31ffb8.jpg"/> sometext here
some text here <img src="web/2a31ffb8.png"/> sometext here
some text here <img src="web/2a31ffb8.jpeg"/> sometext here

Upvotes: -1

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

sed -i 's:\(img src="\).*\(/[^"/]\+\.[^"]\+"\):\1web\2:' INPUTFILE

Might do it in place.

HTH

Upvotes: 1

Related Questions