Re: DOS newlines (CR/LF) to Unix format [message #171733 is a reply to message #171731] |
Wed, 19 January 2011 11:25 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma:
|
Senior Member |
|
|
El 19/01/2011 11:59, Bjarne Jensen escribió/wrote:
> I found this snippet on internet:
>
> # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
> awk '{sub(/\r$/,"");print}' # assumes EACH line ends with Ctrl-M
>
> It works fine on the commandline so I wrote like this in a php-script:
>
> - - -
>
> $unix = ".unix";
>
> foreach (glob($usedir."[12_]*") as $filename) {
> exec('awk { sub("/\r$", ""); print } $filename > $filename.$unix');
> }
>
> - - -
>
>
> But absolutely nothing happens!
>
> Why not?
Read this:
http://www.php.net/manual/en/language.types.string.php
The important bit is that in PHP single quoted strings are different
from double quoted strings (it's exactly the same as in bash). Single
quoted strings are stored as-is, so 'foo $bar' contains exactly that:
foo $bar.
You can check the exact command you are running by using the ancient
debug technique of printing stuff with an echo statement:
foreach (glob($usedir."[12_]*") as $filename) {
echo 'awk { sub("/\r$", ""); print } $filename > $filename.$unix');
}
Now, injecting unescaped random stuff in a command line can be very
dangerous. You should always use escapeshellarg()!
Last but not least... I assume you are using PHP code to launch bash
code because you don't have a full script in either language to do the
full task, am I correct? Your approach is weird and probably
inefficient. Google for "xargs awk", you'll probably find a 100% bash
example.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
|
|
|