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

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

2 Answers 2

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)

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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .