Thursday, February 17, 2022

[SOLVED] finding special characters in array of string variables bash script

Issue

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.


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


Answered By - anubhava
Answer Checked By - Cary Denson (WPSolving Admin)