8
\$\begingroup\$

Your challenge, should you be coded to accept it, runs as follows:

You may choose one of the following two challenges to implement:

Challenge #1

  • Read from a file a.txt
  • Write only printable ASCII characters (values 32-126) to a file b.txt

Challenge #2

  • With a file a.txt, delete all characters in the file except printable ASCII characters (values 32-126)

Specs on a.txt

a.txt is a plain text file which can include any ASCII values from 0-255 (even undefined/control) and its size can range up to 1 GB.

Winning

The answer with the smallest amount of sourcecode in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ @Malachi That's kind of big; how do you suggest I upload it? \$\endgroup\$ – Timtech Jan 22 '14 at 18:37
  • \$\begingroup\$ Your winning criterion is wrong. For example I could just. Add a realy lengthy comment to my code, and win. \$\endgroup\$ – Mhmd Jan 22 '14 at 18:38
  • 2
    \$\begingroup\$ @user689 And so it would be if I had an extremely slow program. I assume he meant that least score wins. \$\endgroup\$ – Oberon Jan 22 '14 at 18:42
  • 1
    \$\begingroup\$ @Malachi but that would mean slower is better, and the question is tagged fastest-code. \$\endgroup\$ – Oberon Jan 22 '14 at 18:43
  • \$\begingroup\$ @amon I'll make it code-golf instead. \$\endgroup\$ – Timtech Jan 22 '14 at 19:08

11 Answers 11

6
\$\begingroup\$

sh

Challenge 1, 24 23 chars

tr -dc \ -~<a.txt>b.txt
Bonus: variations on the set of characters to delete.
tr -dc " -~
"<a.txt>b.txt                       # preserves printables + line feed

tr -dc "[:space:] -~"<a.txt>b.txt   # preserves printables + whitespace
\$\endgroup\$
  • 1
    \$\begingroup\$ You can take out the space before < \$\endgroup\$ – Tobia Jan 24 '14 at 20:26
  • \$\begingroup\$ Right you are! It's just so fugly that I forgot about it. :D \$\endgroup\$ – FireFly Jan 24 '14 at 20:30
1
\$\begingroup\$

Bash + Perl

Challenge 1 – 31 bytes

perl -petr/\ -~//cd a.txt>b.txt

Challenge 2 – 28 bytes

perl -i -petr/\ -~//cd a.txt
\$\endgroup\$
  • \$\begingroup\$ Hmm, it ran and made the b.txt but other characters remain. \$\endgroup\$ – Timtech Jan 22 '14 at 19:19
  • \$\begingroup\$ @Timtech cannot reproduce, vim shows no unprintables for me (when looking at b.txt of challenge 1) \$\endgroup\$ – amon Jan 22 '14 at 19:22
1
\$\begingroup\$

Both are Challenge 1.

C - 132 characters

#include<stdio.h>
k;main(){FILE*f=fopen("a.txt","rb");FILE*g=fopen("b.txt","w");while(!feof(f))(k=getc(f))>31&&k<127&&putc(k,g);}

Python - 78 characters

file("b.txt","w").write(filter(lambda x:'~'>=x>' ',file("a.txt").read()))
\$\endgroup\$
  • \$\begingroup\$ Is this Python 3? \$\endgroup\$ – Timtech Jan 22 '14 at 22:04
  • \$\begingroup\$ The Python one doesn't work with 3.3.3. Edit: It seems you have to make the file first. \$\endgroup\$ – Timtech Jan 22 '14 at 22:15
  • \$\begingroup\$ I ran it on Python 2.7 (r27:82525). \$\endgroup\$ – Oberon Jan 23 '14 at 14:59
1
\$\begingroup\$

Brainfuck: 92

+[[->+>+<<]>>>++++++++[->++++++++++++<<---->]>--[<<[->]>[<]>-]<+<[[-]>-<]>[[-]<<.>>]<<[-]<,]

Brainfuck cannot open files so you use stdin/stdout redirection (#1)

bf strip.bf < a.txt > b.txt

The same code in Extended Brainfuck: 68

+[(->+>+)3>8+(->12+<<4-)>2-[<<[->]>[<]>-]<+<([-]>-)>([-]2<.)2<[-]<,]
\$\endgroup\$
0
\$\begingroup\$

iX3, 388

Set  in [c]:  '1'
Loop start   ([c]  of 1  to 999999999)
   Read from file  'a.txt' into [T] (read from [c], 1 bytes)
   Modify text  '[T]' into [R] (code in Hex)
   Set  in [N]:  '&H[R]' (calculate)
   If  True: '([N]=10)'
      Write into file  'b.txt' line '' (Append)
   End If
   If  True: '([N]>31)&([N]<127)'
      Write into file  'b.txt' line '[T]' (Append, no linefeed)
   End If
Loop end
\$\endgroup\$
0
\$\begingroup\$

PowerShell (43, 43)

Challenge 1: (43)

(gc -raw a.txt) -replace"[^ -x7e]","">b.txt

Challenge 2: (43)

(gc -raw a.txt) -replace"[^ -x7e]","">a.txt
\$\endgroup\$
  • \$\begingroup\$ Why not use ~ instead of x7e? Also you can leave off the ,"" argument and keep the same effect I believe. \$\endgroup\$ – Etan Reisner Apr 15 '15 at 16:53
0
\$\begingroup\$

Challenge #1

Ruby (52)

 IO.write (f="a.txt"),IO.read(f).gsub(/\p{Cntrl}/,'')
\$\endgroup\$
0
\$\begingroup\$

Retina, 7 bytes

Simple regex to remove non-printable ASCII.

[^ -~]

Try it online!

Test input copied from this SO post.

\$\endgroup\$
  • \$\begingroup\$ I think you're missing the read/write to a/b.txt aspect of the challenge \$\endgroup\$ – Jo King Nov 25 '18 at 1:36
  • 1
    \$\begingroup\$ @JoKing Retina cannot open files. Same as this answer, so it's okay. \$\endgroup\$ – mbomb007 Nov 25 '18 at 5:26
0
\$\begingroup\$

Tcl, 122 123 bytes

set g [open b.txt w]
lmap c [split [read [open a.txt]] ""] {scan $c %c v
if $v>31&$v<127 {puts -nonewline $g $c}}
close $g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

K4, 36 33 bytes

Solution:

`b.txt 1:{x@&31<x*127>x}@1:`a.txt

Explanation:

Above is for Challenge A, replace the b.txt for a.txt for Challenge B.

`b.txt 1:{x@&31<x*127>x}@1:`a.txt / the solution
                         1:`a.txt / binary read (1:) "a.txt"
                        @         / apply to 
         {             }          / lambda taking 1 implicit arg (x)
                  127>x           / 127 greater than?
                 *                / multiply by
                x                 / x
             31<                  / 31 less than?
            &                     / indices where true
          x@                      / index into x at these indices
`b.txt 1:                         / binary write to "b.txt"
\$\endgroup\$
0
\$\begingroup\$

Burlesque - 21 bytes

{**{31.>}{127.<}m&}f[

This can be shortened be replacing the 128 with '<DEL> (where DEL is the delete character) but I'm not sure how I can post a DEL here (also replace the 31 and then you can also get rid of the **)).

Also, in the WIP version you can golf this down to :un:ln.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.