FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Re: DOS newlines (CR/LF) to Unix format
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: DOS newlines (CR/LF) to Unix format [message #171733 is a reply to message #171731] Wed, 19 January 2011 11:25 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  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
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: fatal error: cannot redeclare
Next Topic: getting file from <option>
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 16:40:21 GMT 2024

Total time taken to generate the page: 0.07953 seconds