The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. November 21, 2008, 04:34:13 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: In need of a script to find and replace unicode characters on a single file  (Read 533 times)
skyts
Rookie
**
Offline Offline

Posts: 22


View Profile
« on: June 26, 2007, 09:28:38 AM »

I need something that can run from shell.
I have tried using "sed" command, But it does not work if you need to replace unicode characters.

Will give a big thank you if you know a script or a command that can do this.

example of what I need:

replace "á","à" file.txt
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5142


:sniffle: Humor was so much easier before.


View Profile
« Reply #1 on: June 26, 2007, 03:05:28 PM »

There certainly is a *nix command line to do that stuff, but I don't know it. Wait for TheDarkness to wake up and he might have a thought.

In the mean time, you can make an executable PHP script as your own command line utility. Consider:

Code:
#! /usr/local/bin   <---- this points to YOUR php instance (find it with "which php")
<?php

$inputFile 
$_SERVER['argv'][1];
$originalStr $_SERVER['argv'][2];
$replaceStr $_SERVER['argv'][3];

$buff file_get_contents($inputFile);
file_put_contents($inputFilestr_replace($originalStr$replaceStr$buff));

?>


Assuming you put this code into a file name "convert.php" you'd first need to make it executable with

chmod 755 convert.php

and then to execute it you'd something like:

./convert.php theInputFile.txt crap crud

This example would convert every instance of "crap" in a file and convert it to "crud," writing the output back to the original file. Clearly this is an overly simple example, and you'd probably want to add checking to make sure the input file is OK, or specify an optional output file, or perhaps the ability to say:

./convert.php inputFile.txt &#212 O

... which is a captial "O" with a circumflex, converted to a normal capital O. Or you could add FILES as input for search and replace, so that you could have lots of things to convert all in one pass. The possibilities are endless.

Hope this helps, ping back if you need more,
/p
Logged

If I can't be Mr. Root then I don't want to play.
skyts
Rookie
**
Offline Offline

Posts: 22


View Profile
« Reply #2 on: June 27, 2007, 03:50:59 AM »

Thanks Perkiset, But I don't think this will work for unicode characters... To find and replace words on a file I can just use the "Sed" command in unix. I need a command line editor that is able to edit unicode characters from shell...

But you are always a big help, Thanks Smiley
Logged
thedarkness
Global Moderator
Lifer
*****
Offline Offline

Posts: 580



View Profile
« Reply #3 on: June 27, 2007, 06:38:39 PM »

OK, I'm awake now ;-)

You can do this with tr or sed and prolly some others, but maybe a full explanation of what you want to do and why is in order, we may have better ideas with more info.

echo á| tr \\341 \\340

like I said though, exactly what you want to do and why will prolly get you a better solution.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!