skyts

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

perkiset

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:


#! /usr/local/bin  <---- this points to YOUR

php

  instance (find it with "which

php

 "Applause
<?

php

 

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

$buff = file_get_contents($inputFile);
file_put_contents($inputFile, str_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

skyts

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 Applause

thedarkness

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


Perkiset's Place Home   Politics @ Perkiset's