tags:

views:

220

answers:

6

So I'm participating in one of the Code Golf competitions where the smaller your file size is, the better.

Rather than manually removing all whitespace etc, I'm looking for a program or website which will take a file, remove all whitespace (including new lines) and return a compact version of the file. any ideas?

+7  A: 

you could use

sed 's/\s\s+/ /g' youfile > yourpackedfile

there is also this online tool

you can even do it in PHP (how marvelous is life)

$data = file_get_contents('foobar.php');
$data = preg_replace('/\s\s+/', ' ', $data);
file_put_contents('foobar2.php',$data);

You have to note this won't take care of string variable like $bar = ' asd aa a'; it might be a problem depends on what you are doing. The online tool seems to handle this properly.

RageZ
Fabulous, the online tool works great
Click Upvote
@Click Upvote: great!
RageZ
I'd prefer `/\s{2,}/` over what you've got there. I'm surprised a PHP code minifier exists... most of the time there's no point; it's server-side.
Mark
@Mark: the only point might be those competition. yeah `\s{2,}` would be more elegant.
RageZ
+1  A: 

In PowerShell (v2) this can be done with the following little snippet:

(-join(gc my_file))-replace"\s"

or longer:

(-join (Get-Content my_file)) -replace "\s"

It will join all lines together and remove all spaces and tabs.

However, for some languages you probably don't want to do that. In PowerShell for example you don't need semicolons unless you put multiple statements on a single line so code like

while (1) {
    "Hello World"
    $x++
}

would become

while(1){"HelloWorld"$x++}

when applying aforementioned statements naïvely. It both changed the meaning and the syntactical correctness of the program. Probably not too much to look out for in numerical golfed solutions but the issue with lines joined together still remains, sadly. Just putting a semicolon between each line doesn't actually help either.

Joey
+2  A: 
$ tr -d ' \n' <oldfile >newfile
Useless
A: 

if your code editor programs supports regex, you can try this:

Find this: [\r\n]{2,} Replace with this: \n Then Replace All

Sarfraz
A: 

Notepad++ is quite a nice editor if you are on windows, and has a lot of predefined macros, trimming down code and removing whitespace among them. It can do regular expressions and has a plethora of features to help the code hacker or script kiddie notepad++ website

myk_raniu
A: 

run php -w on it!

php -w myfile.php

Unlike a regex, this is smart enough to leave strings alone, and it removes comments too.

nickf