Linux Rocks ! wrote:
>
> I do have a question about grep and stuff... I would like to replace text
>with different text (like change alt= tags to title= or duplicate alt tags to
>title tags... so, If i have something like
>
>
>and I want
><a href="www.rocksolidnetworks.com"><img src="rock.jpg" alt="picture of rock"
>title="picture of a rock"></a>
>
>What is the easiest way (yes, I could read awk, and sed and grep...) but I
>basicly just want a rename for inside files (not just filenames).
>
Both vi and emacs provide a regular expression replace mechanism. In
vi, try:
:%s/<regex to find>/<replacement>/g
(Note that the command starts with a colon)
I'n emacs, it's C-M-%, or access it from the edit menu.
When I want to do something more sophisticated, I'll usually write a
quick Perl script (or one-liner), which is how I would do what you want
above:
cat file.html | perl -ne 's/alt\s*=\s*"(.*?)"/alt="$1" title="$1"/; print'
This says: cat the file into perl, and for each line (-n option),
evaluate (-e option) the script in the single quotes:
susbstitute(s/<regex>/<replacement>/):
alt<optional whitespace>=<optional whitespace>"<any character in
between the quotes, save it in $1>"...
replace with:
alt="<whatever matched in the parentheses" title="<whatever matched
in the parentheses"
print the altered string (to stdout)
Try it and see if it works for you... You can do the same stuff with sed
or awk.
Kahli