简体   繁体   中英

finding special characters in array of string variables bash script

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM