1

I want to write bash script for search, copy and paste an element by its class name. I am quite new to bash. Can anybody help me or give me a suggestion?

For example,

<div class="elementOne">..</div>
<div class="elementTwo">
   <div class="SubelementOne">....</div>
   <div class="SubelementTwo">....</div>
</div>

I want to select "SubelementTwo" and put it under "elementOne"

Dharmesh Rupani
  • 1,029
  • 2
  • 12
  • 22

1 Answers1

3

First turn your HTML into XHTML, then do any modifications you want with an XML tool.

If you have an input like this:

$ cat foo
<div class="elementOne">..</div>
<div class="elementTwo">
   <div class="SubelementOne">....</div>
   <div class="SubelementTwo">....</div>
</div>

Clean it up into valid XHTML with tidy and then easily move elements with class SubelementTwo under an element with class elementOne:

$ tidy -asxhtml foo 2>/dev/null | \
    xmlstarlet ed -m '//*[@class="SubelementTwo"]' '//*[@class="elementOne"]'

<?xml version="1.0"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.2.0"/>
    <title/>
  </head>
  <body>
    <div class="elementOne">..<div class="SubelementTwo">....</div></div>
    <div class="elementTwo">
      <div class="SubelementOne">....</div>
    </div>
  </body>
</html>

This is more robust than regex based solutions.

Community
  • 1
  • 1
that other guy
  • 116,971
  • 11
  • 170
  • 194