3

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

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

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

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)

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.