Question

It's regarding finding words with special characters in a text file and separate them.

For ex: I have a file called temp.txt with below values. File can contain n number of values but I just mentioned 4 as an example.

Ag$thyg
bacde
\RTysdre
stack

So, I want values WITHOUT special characters to be stored in another file.

Was it helpful?

Solution

You can use this sed probably:

sed -i.bak 's/[^a-zA-Z0-9_-]*//g' file

This removes every character that is not alpha num OR underscore or hyphen.

But I guess you need to elaborate your definition of special character better.

EDIT: As per comments you can use grep:

grep -v '[^a-zA-Z0-9_-]' file > newfile

OR else:

egrep '^[a-zA-Z0-9_-]+$' file > newfile

OTHER TIPS

Use tr like this:

tr -d "[^a-zA-Z0-9]" < yourfile

and list inside the square braces any extra characters you particularly dislike! At the moment, tr will delete (-d) anything that is not upper and lower case letters and digits.

If you want to save the output to a new file, do this:

tr -d "[^a-zA-Z0-9]" < yourfile > newfile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top