1

Homage to scriptings gurus. I have a really polstergeist phenomenal on virtual machine. I have a script to change the url from image. In a normal linux OS this work normal with SED but in virtual machine (I prove 2 differents machines and differents configurations with differents terminals too) i have differents results. This is the case:

My original line

<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

My good result in Linux Native:

<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='/home/valentin/recetas/public_html/convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

My fault result on Virtual Machine

<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='/home/valentin/recetas/public_html//home/valentin/recetas/public_html/convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

The SED command:

 sed -i "s|img src='|img src='${BASEDIR}/${PUBLIC_BASE}/|" "${i}"

Where

BASEDIR=/home/valentin/recetas
PUBLIC_BASE=public_html

I tried without vars . I thought the problem can be on the quotes in the pattern sustitution and final slash but i try with differents options and without variables without positive result. The result final i need is the good result. Really i don't know how to made no duplicate url on the PATTERN replace with sed. Or if is not possible how i can to extract this second duplicate string. Thank in advance.

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Do you have any links to the same file? The only way I can see this happening is if you execute the command twice on the same file. – Barmar Oct 21 '22 at 23:31
  • Please [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858). I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Oct 21 '22 at 23:52

1 Answers1

0

Is it possible you could have run the sed -i twice?

I can (obviously?) duplicate the issue by running the sed -i call twice, eg:

$ cat url
<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

$ sed -i "s|img src='|img src='${BASEDIR}/${PUBLIC_BASE}/|" url
$ cat url
<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='/home/valentin/recetas/public_html/convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

$ sed -i "s|img src='|img src='${BASEDIR}/${PUBLIC_BASE}/|" url
$ cat url
<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='/home/valentin/recetas/public_html//home/valentin/recetas/public_html/convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

One idea for removing the duplicates consists of removing all instances of ${BASEDIR}/${PUBLIC_BASE}/ and then re-adding a single instance:

$ sed -i "s|${BASEDIR}/${PUBLIC_BASE}/||g; s|img src='|img src='${BASEDIR}/${PUBLIC_BASE}/|" url
$ cat url
<div class="col-sm-6 col-md-4 col-lg-3"><figure> <img src='/home/valentin/recetas/public_html/convert_IMG-20181121-WA0009.jpg' class="img-fluid galeria"></figure></div>

This could also be used against the original url with the understanding that the first replacement (s|${BASEDIR}/${PUBLIC_BASE}/||g) will be a no-op.

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • 1
    This was obviosly , but i have my doubts because if i tried to made this i thought i will stay in the same position that before the sed command. But i tried and OK! It's work for me, the only difference its the slash but it's correct your solution. Thank you Mark for your quickly and effective response. This is the final sentence: ` sed -i "s|${BASEDIR}/${PUBLIC_BASE}/||g; s|img src='|img src='${BASEDIR}/${PUBLIC_BASE}/|" "${i} ` – Valentin Luque Oct 22 '22 at 00:24