Take the 2-minute tour ×

I have a large text file that has a lot of special characters in it like "$!@%#$/" plus many more and I would like to remove the line in the text file if it has any special characters in that line. The only characters I want to keep is a-z and A-Z.

If this was the file...


Then the only thing that would be left would be...


A solution with linux command line tools, or a bash script, or a python script would be better, but anything that works will do!

share|improve this question

7 Answers 7

up vote 1 down vote accepted

If you want to keep ONLY lines with alpha characters (as the OP requested), then:

$ grep -v '[^a-zA-Z]' foo

Or if you only want English characters:

$ grep -v '[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]' foo

But if you just want to remove non-alpha characters, sed will do the job:

$ cat foo | sed 's/[^a-zA-Z]//g'

Or if you just want to kill binary, non-printable data, use strings:

$ strings foo
share|improve this answer
The sed command would only make the unwanted lines blank. To prevent printing the them you'd need to tell sed when to print; sed -n '/^[a-zA-Z]\+$/p'. This is equivalent to the grep though, and probably slower. –  sebnow Oct 29 '11 at 5:26
This works how I was thinking! Thank-you so much! –  Elmer Oct 29 '11 at 21:34
which one does what you want? –  ObscureRobot Oct 29 '11 at 21:41
Well I thought the grep did. But I just looked at the file really close and found ëöü characters still in it... –  Elmer Oct 29 '11 at 21:44
accented latin characters probably fall within the a-z range, so if you only want english characters try: $ grep -v '[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]' foo –  ObscureRobot Oct 29 '11 at 21:51
$ grep '^[[:alpha:]]\+$' << EOF
> !Somejunk)(^%
> )%(&_
> this
> my_file
> is
> *(%%$
> the
> they're
> file
share|improve this answer
Or grep -v '[^[:alpha:]]', which is even shorter. –  Johnsyweb Oct 29 '11 at 6:48
The regex is shorter, but the command itself is the same length. –  Ignacio Vazquez-Abrams Oct 29 '11 at 12:05

This seems to work:

 sed '/[^[:alpha:]]/d' source_file
share|improve this answer
If I could up-vote I would because this also works. Thank-you! –  Elmer Oct 29 '11 at 21:39

grep -v and some regex?

say, egrep -v '[^a-zA-Z]'

share|improve this answer
This works as well, but I cannot vote. Thank-you so much! –  Elmer Oct 29 '11 at 21:39

You can use the following command to filter out the required lines:

grep '^[A-Za-z ]\+$' file

If you don't even allow spaces in rows, you may omit out space after z.

share|improve this answer

or like this completely in bash


file=$(cat file.txt);

for line in $file; do
    if [[ $line =~ ^[a-zA-Z]+$ ]]; then
        echo $line
share|improve this answer

I'm going to take the really nooby approach.

x = open('file','r')
y = x.read().split('\n')

for z in range (0, len(y)):
    for a in range (0, len(y[z])):
        if not y[z][a].isalpha() and not y[z][a].isdigit():
            y[z][a] = ''

OutputString = '\n'.join(y)
share|improve this answer

Your Answer


