1

I have a file, events.html, containing similar lines (among many other):

<td class="EventDate">2021-08-06 12:36:34</td>

where date and time strings are different in each line.
In these lines there are ONLY class="EventDate"
Like to remove from every such line ONLY time string.

Additional info:

  • system is Debian 9.13 64 bit
  • file is 38kb long
  • there are about 100 records, each comprising line similar to
    <td class="EventDate">2021-08-06 12:36:34</td>
    BUT date and time strings are different in each line!
  • file contain near the end only one line with (example!)
    date and time: [2021-08-08 14:16:54 UTC]

For security reasons can not post whole file.

I am neither IT professional nor Linux Guru
but capable of performing various demanding tasks.

0

1 Answer 1

2

Using xmlstarlet to edit the file, updating the value with an xpath expression:

xmlstarlet ed -u '//td[@class="EventDate"]' -x 'substring-before(.," ")' events.html

Add -L to perform the edits in-place instead of writing the result to standard output.

Ex. given

$ cat events.html 
 <table>
  <tr>
    <td>Cell A</td>
    <td class="EventDate">2021-08-06 12:36:34</td>
  </tr>
  <tr>
    <td>Cell C</td>
    <td class="OtherDate">2021-08-06 12:36:34</td>
  </tr>
</table>

then

$ xmlstarlet ed -O -u '//td[@class="EventDate"]' -x 'substring-before(.," ")' events.html 
<table>
  <tr>
    <td>Cell A</td>
    <td class="EventDate">2021-08-06</td>
  </tr>
  <tr>
    <td>Cell C</td>
    <td class="OtherDate">2021-08-06 12:36:34</td>
  </tr>
</table>
6
  • To steeldriver... Your recipe generally is O.K. but it complains about incorrect sequence of HTML tags. Checking my file with validator.w3.org/nu prints only one error "Element marquee not allowed as child of element span in this context." Need to modify a bit events.html file to get your solution working. If I remove offending lines, solution works perfectly O.K! Aug 9, 2021 at 11:32
  • @TeWuEsTewues thanks for the feedback - perhaps there are elements that are valid HTML but invalid XML (you could see what xmlstartlet val events.html says about that) Aug 9, 2021 at 12:41
  • Thre's typo "xmlstar t let"... Says not much: user@linux:/home/user$ xmlstarlet val events.html events.html - invalid user@linux:/home/user$ Still digging - Thank you. Aug 9, 2021 at 13:56
  • You can preprocess the HTML data into XML compliance using xmlstarlet fo -H file.html 2>/dev/null. Feed the output of this into the given xmlstarlet ed …
    – roaima
    Aug 9, 2021 at 16:18
  • 1
    @roaima thanks that's a good suggestion - pity we don't have a more representative input sample to try Aug 9, 2021 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.