6

I have a text file that looks like the text that is pasted below. I want to extract to a file any characters that are alphanumeric in nature, and ignore everything else.

What is the easiest way to do this (grep, cut)?

%[{]$#{!^]^$#+*$}#*)(]!@^&#){][$)}!+%^)@#&!%(+^^($(%}^+[*)#+{%!))}(*&]__})][_))}#
%())#&##{]$#$](&$%&&$)^{(@%)$%()#)&&*{]&^^+%$##%{!(_$(**&(_]+{%[%$!_){$*@@++]&^$(
%@+{+&%]$)+@({$(+{!*#(%)]+[}){]]#)*[]%&{+)$){!&$]+^++_@]#%)[&&^%]#@#@)]@}%$[_*@%)
%[&*^*})@(!{&^#!([%@_![{)+)$}_+)%&^#@#$$}))^&)}({+*&_()&@]$^#(&&{){)_[}{@(}#)!)%&
%({+$[!#()[]%{$_*]*^%&]@{^@{)}}_^}@!^*)_[([{}]{*#{]&}}[$_[}!%%&_{{!$[}&[[@#[&_$()
%*_$+)&}*){${}!]+%[{{!+)+{!&]$!}{_]&)!!^+){&*#{@!##_(^%^$([!+&+($&)##[&[^_{##{(**
%{{)#*%@*[(^(}!%}@*}@+]^_}&&&}&{[$(@[#*+%[&%{$$**]]%(!$+$!]^+[^_(&*{#_^%[[#+{]#_[
%*}]#)!%!_[})^%*@{!{$)*_+$$*}%(&]%^+$@!&{[]}**})}#}[#{%{$#@##(])&)((${^]^[%^&(!_&
jro
  • 61
  • I may have answered my own question. Here's the command I used: 'grep -oE "[[:alnum:]]" findnormalchars.txt' – jro Jan 19 '16 at 00:29
  • Sorry if this was a bad question, first time posting. Working through bash scripting exercises. – jro Jan 19 '16 at 00:31

2 Answers2

6

For ASCII alphanumeric characters only:

LC_ALL=C tr -cd '[:alnum:]' <file

For alphanumeric characters according to your locale, just:

tr -cd '[:alnum:]' <file

or defining the locale explicitly:

LC_ALL=en_US.UTF-8 tr -cd '[:alnum:]' <file

(An exception is GNU tr, which does not support multi-bytes characters currently)

cuonglm
  • 153,898
5

I don't see any alphanumeric characters in there, but this sed command should work sed 's/[^a-zA-Z0-9]//g' /tmp/foobar where /tmp/foobar is the input file.

s/a/b/ means replace any instance of a with b, [^a-z] means any character that is not a through z.s/a/b/g means do this globally, rather than stopping at the first instance on each line, keep on doing this.

Jacob Minshall
  • 4,826
  • 2
  • 17
  • 10
  • for a really in depth look into regular expressions, I'd recommend checking this book out: http://shop.oreilly.com/product/9780596528126.do :) – Jacob Minshall Jan 27 '16 at 08:20