sed: removing alphanumeric words from a file

daydreamer picture daydreamer · Dec 13, 2010 · Viewed 26.2k times · Source

I have file with a lot of text, what I want to do is to remove all alphanumeric words.

Example of words to be removed:

gr8  
2006  
sdlfj435ljsa  
232asa  
asld213  
ladj2343asda
asd!32  

what is the best way I can do this?

Answer

Dennis Williamson picture Dennis Williamson · Dec 14, 2010

If you want to remove all words that consist of letters and digits, leaving only words that consist of all digits or all letters:

sed 's/\([[:alpha:]]\+[[:digit:]]\+[[:alnum:]]*\|[[:digit:]]\+[[:alpha:]]\+[[:alnum:]]*\) \?//g' inputfile

Example:

$ echo 'abc def ghi 111 222 ab3 a34 43a a34a 4ab3' | sed 's/\<\([[:alpha:]]\+[[:digit:]]\+[[:alnum:]]*\|[[:digit:]]\+[[:alpha:]]\+[[:alnum:]]*\) \?//g'
abc def ghi 111 222