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\$
10
  • \$\begingroup\$ @Malachi That's kind of big; how do you suggest I upload it? \$\endgroup\$
    – Timtech
    Commented Jan 22, 2014 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\$
    – Mohammad
    Commented Jan 22, 2014 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
    Commented Jan 22, 2014 at 18:42
  • 1
    \$\begingroup\$ @Malachi but that would mean slower is better, and the question is tagged fastest-code. \$\endgroup\$
    – Oberon
    Commented Jan 22, 2014 at 18:43
  • \$\begingroup\$ @amon I'll make it code-golf instead. \$\endgroup\$
    – Timtech
    Commented Jan 22, 2014 at 19:08

13 Answers 13

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\$
2
  • 1
    \$\begingroup\$ You can take out the space before < \$\endgroup\$
    – Tobia
    Commented Jan 24, 2014 at 20:26
  • \$\begingroup\$ Right you are! It's just so fugly that I forgot about it. :D \$\endgroup\$
    – FireFly
    Commented Jan 24, 2014 at 20:30
2
\$\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\$
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\$
2
  • \$\begingroup\$ Hmm, it ran and made the b.txt but other characters remain. \$\endgroup\$
    – Timtech
    Commented Jan 22, 2014 at 19:19
  • \$\begingroup\$ @Timtech cannot reproduce, vim shows no unprintables for me (when looking at b.txt of challenge 1) \$\endgroup\$
    – amon
    Commented Jan 22, 2014 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\$
3
  • \$\begingroup\$ Is this Python 3? \$\endgroup\$
    – Timtech
    Commented Jan 22, 2014 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
    Commented Jan 22, 2014 at 22:15
  • \$\begingroup\$ I ran it on Python 2.7 (r27:82525). \$\endgroup\$
    – Oberon
    Commented Jan 23, 2014 at 14:59
1
\$\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\$
1
  • \$\begingroup\$ Why not use ~ instead of x7e? Also you can leave off the ,"" argument and keep the same effect I believe. \$\endgroup\$ Commented Apr 15, 2015 at 16:53
1
\$\begingroup\$

Vyxal, 6 bytes

kPs5ȯ↔

Try it Online!

Fixed thanks to Aaron Miller. Why does the printable ascii builtin contain unprintables?

\$\endgroup\$
1
  • \$\begingroup\$ This should work, but unfortunately, the kP builtin contains two unprintables (for some reason), plus the challenge specifies only keeping ASCII 32-126, which doesn't include newlines. The shortest I was able to get it while keeping in line with that range is 6 bytes \$\endgroup\$ Commented Aug 5, 2021 at 17:24
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\$

Retina, 7 bytes

Simple regex to remove non-printable ASCII.

[^ -~]

Try it online!

Test input copied from this SO post.

\$\endgroup\$
2
  • \$\begingroup\$ I think you're missing the read/write to a/b.txt aspect of the challenge \$\endgroup\$
    – Jo King
    Commented Nov 25, 2018 at 1:36
  • 1
    \$\begingroup\$ @JoKing Retina cannot open files. Same as this answer, so it's okay. \$\endgroup\$
    – mbomb007
    Commented Nov 25, 2018 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\$
0
\$\begingroup\$

Japt, 3 bytes

Well, seeing as everyone else is ignoring the requirements to read from & write to a file ...

Input also from this SO answer.

;oE

Try it

Japt v2.0a0, 3 bytes

r\P

Try it

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

Ruby (52)

Challenge #1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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