regex find & replace command ?

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 25th, 2008, 6:47 am

I'm looking for somthing I can pass a filename, perl-compatible regular expression, & replacement pattern to for find & replace via a terminal.

Somthing like this.

Code: [ Select ]
command /path/to/file.html "~(<link\s+[^>]*href=['\"])[^'\"]*styles\.css['\"]~i" "$1newstyles.css"


Or this

Code: [ Select ]
command /path/to/file.html "~\s+$~mi" ""
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 25th, 2008, 6:47 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post March 26th, 2008, 3:57 pm

This kinda makes me want to write this up myself.

If I get some free time this weekend, I think I will.
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 26th, 2008, 10:11 pm

This isn't exactly what I was looking for, but it works & uses a regex syntax I'm familiar with.

Uses the PHP CLI to pass arguments to a preg_replace script.

replace.php
Code: [ Select ]
<?php
    echo preg_replace($argv[2], $argv[3], file_get_contents($argv[1]));
?>
  1. <?php
  2.     echo preg_replace($argv[2], $argv[3], file_get_contents($argv[1]));
  3. ?>


usage
Code: [ Select ]
php -f replace.php test.html "~(<link\ef\s*=\s*['\"][^'\"]*)/style\.css~si" '$1/new.css' > test2.txt
Strong with this one, the sudo is.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post March 26th, 2008, 10:20 pm

That was my first thought, but I figured you were looking for a small executable that you could add to your PATH.
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 27th, 2008, 7:00 am

Actually I think the small executable, or the source for one, is what I'm looking for.
Strong with this one, the sudo is.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post March 31st, 2008, 8:39 pm

I'm looking into the Boost regex library for this, since C++ doesn't have any built-in Perl-style regex capabilities. As soon as I get the chance to play around with it I'll put together a small program.
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post April 12th, 2008, 9:11 pm

While getting to know gedit I came across sed which I can use via gedits external applications plugin.

Attachments:
gedit-sed.png

An example of sed being used to strip whitespace from the end of lines in a document.



The syntax for sed is slightly different than what I'm used to, but since I can save commands as tools & use them later I think I can work around the syntax.
Strong with this one, the sudo is.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post April 14th, 2008, 8:22 pm

Interesting. I still plan on making this small app when I get the chance, I've just been insanely busy lately.

That sed looks like it uses POSIX regex syntax.
The Beer Monocle. Classy.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 46 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.