Regular Expression Puzzle

Loïc Grenié loic.grenie at gmail.com
Sun Nov 29 13:39:02 UTC 2009


2009/11/29 Alan McKay <alan.mckay at gmail.com>:
> Dude, that's a bit of a convoluted way to go about it.
>
> How about keeping it simple?
>
> cat file | sed "s/<div>/<p>/g" | sed "s/<\/div>/<\/p>/g" > newfile

   even simpler:

sed "s/<div>/<p>/g" file  | sed "s,</div>,</p>,g" > newfile

   Variants:

   (one sed, two regexps)
sed -e "s/<div>/<p>/g" -e "s,</div>,</p>,g" file > newfile

   (only one regexp)
sed "s,<\(/\?\)div>,<\1p>,g" file > newfile

  (shown only for the last one, but it can be used for any,
  change in place)
sed -i "s,<\(/\?\)div>,<\1p>,g" file
  in that case the file "file" is changed, non new file is created.

       Loïc




More information about the ubuntu-users mailing list