Question

I have multiple html files. In these, there are accesskeys set to next and previous page. The links to these 2 pages are placed in the header and in the footer. Because of the duplicity, the accesskey is not working as it should (i.e., open the page).

The solution is to remove the links from header. The header is written in one line, so the two links I want to delete are in the 2nd line.

Because it contains the name of the file that is to be opened, I need to create either a var to these file names or some other, more complex command (the following one I use to rename all files to ### {000..999}). I donno if the following complex cmd alter the output name of the html files (it is not desired)

$ ls *.html | awk 'BEGIN{ a=0 }{ printf "sed -i '2!b;s/<a accesskey="p" href="%s">Prev</a>/' *.html  %s\n", $0, a++ }' | bash


Only the sed cmds (for clarity):

$ sed -i '2!b;s/<a accesskey="p" href="%s">Prev</a>/' *.html

$ sed -i '2!b;s/<a accesskey="n" href="%s">Next</a>/' *.html


How can this be accomplished? I tried searching websites (probably I just donno how to describe this), tried many commands with many options, but I just cannot get it :)

Update #2
Sample input

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"><td width="20%" align="left"><a accesskey="j" href="contrastchapter.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Contrasts</th><td width="20%" align="right"> <a accesskey="k" href="ch03s02.html">Next</a></td></tr></table><hr/>


Sample output of the same

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"><td width="20%" align="left"></td><th width="60%" align="center">Chapter 3. Contrasts</th><td width="20%" align="right"></td></tr></table><hr/>
Était-ce utile?

La solution

I'm not sure what you are trying to do exactly. Your shell example doesn't execute. I think you want to delete, for example,

<a accesskey="j" href="contrastchapter.html">Prev</a>

and

<a accesskey="k" href="ch03s02.html">Next</a>

in line 2 in your html files.

If so, why not try

#!/bin/sh                                                                                                                                    
ls *.html | while read line
do
    sed -i  ' 1,2 {                                                                                                                      
            s:<a accesskey="j" href="[[:alnum:].]*">Prev</a>::                                                                               
            s:<a accesskey="k" href="[[:alnum:].]*">Next</a>::                                                                               
        }' $line

done

I'm assuming filenames consist of alphanumerics or a period... You may have to tweak this

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top