regex find & replace command ?

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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" ""
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

This kinda makes me want to write this up myself.

If I get some free time this weekend, I think I will.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

That was my first thought, but I figured you were looking for a small executable that you could add to your PATH.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Actually I think the small executable, or the source for one, is what I'm looking for.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

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.

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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.