Though this goes against my better judgment I'll post it (sed
part).
That is: if it is for a quick and dirty fix go ahead. If it is a bit more
serious or something you are going to do frequently etc. Use something else
like python, perl etc. where you do not rely on regular expressions, but rather modules to handle HTML documents.
One of the simpler ways would be to use e.g. sed.
sed 's/\(<[^>]*\) \+id="[^"]*"\([^>]*>\)/\1\2/' sample.html > noid.html
Explained:
+--------------------------------- Match group 1
| +---------- Match group 2
___|___ ___|___
| | | |
sed 's/\(<[^>]*\) \+id="[^"]*"\([^>]*>\)/\1\2/' sample.html > noid.html
| | | | | | | || | | |
| | | | | | | || | | +- \1\2 Subst. with group 1 and 2
| | | | | | | || | +-------- > Closing bracket
| | | | | | | || +----------- [^>]* Same as below
| | | | | | | |+---------------- " Followed by "
| | | | | | | +----------------- * Zero or more times
| | | | | | +------------------- [^"] Not double-quote
| | | | | +------------------------ id=" Literal string
| | | | +--------------------------- \+ Space 1 or more times
| | | +------------------------------- * Zero or more times
| | +--------------------------------- [^>] Not closing bracket
| +------------------------------------ < Opening bracket
+---------------------------------------- s Substitute
Use sed -i
to edit file in place. (Regrets possible but no undo.)
Better; example using perl:
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser::Simple;
use HTML::Entities;
use utf8;
die "$0 [file]\n" unless defined $ARGV[0];
my $parser = HTML::TokeParser::Simple->new(file => $ARGV[0]);
if (!$parser) {
die "No HTML file found.\n";
}
while (my $token = $parser->get_token) {
$token->delete_attr('id');
print $token->as_is;
}
Your grep command would match nothing. But as you use the invert option -v
it
prints everything not matching – thus the entire file.
grep is not a in place file modifier but normally a tool for finding stuff
in file(s). Try e.g.:
grep -o '\(<[^>]*\)id="[^"]*"[^>]*>' sample.html
-o
means print only matching pattern. (Not whole line)
sed
, awk
etc. are often used to edit streams or files. E.g. as by example above.
From your grep there is a few miss-conceptions:
id\="[a-zA-Z][0-9]"
Would match exactly:
id=
- One character in the range
a-z
or A-Z
- Followed by one single digit
In other words it would match:
id="a0"
id="a1"
id="a2"
...
id="Z9"
Nothing like: id="foo99"
or id="blah-gah"
.
Further it would match:
^ <-- start of line (As it is first in pattern or group)
$ <-- end of line (As you use the `-E` option)
# Else it would be:
^ <-- start of line (As it is first in pattern or group)
$ <-- dollar sign (Does not mean end of line unless it is at end of
pattern or group)
Thus nothing.
.html
. Its been awhile but something likeawk '// || // *.html > *.html
comes to mind.