# Strip all non-printable ASCII characters

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.

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

## 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

• You can take out the space before < – Tobia Jan 24 '14 at 20:26
• Right you are! It's just so fugly that I forgot about it. :D – FireFly Jan 24 '14 at 20:30

## Bash + Perl

### Challenge 1 – 31 bytes

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


### Challenge 2 – 28 bytes

perl -i -petr/\ -~//cd a.txt

• Hmm, it ran and made the b.txt but other characters remain. – Timtech Jan 22 '14 at 19:19
• @Timtech cannot reproduce, vim shows no unprintables for me (when looking at b.txt of challenge 1) – amon Jan 22 '14 at 19:22

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()))

• Is this Python 3? – Timtech Jan 22 '14 at 22:04
• The Python one doesn't work with 3.3.3. Edit: It seems you have to make the file first. – Timtech Jan 22 '14 at 22:15
• I ran it on Python 2.7 (r27:82525). – Oberon Jan 23 '14 at 14:59

# 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<[-]<,]


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

• Why not use ~ instead of x7e? Also you can leave off the ,"" argument and keep the same effect I believe. – Etan Reisner Apr 15 '15 at 16:53

## iX3, 388

Set  in [c]:  '1'
Loop start   ([c]  of 1  to 999999999)
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


# Ruby (52)

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


# Retina, 7 bytes

Simple regex to remove non-printable ASCII.

[^ -~]



Try it online!

Test input copied from this SO post.

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

# 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!

# 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"


## 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.