简体   繁体   English

在字符串变量数组中查找特殊字符bash脚本

[英]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.例如:我有一个名为temp.txt的文件,其中包含以下值。 File can contain n number of values but I just mentioned 4 as an example.文件可以包含n个值,但我只是以 4 个为例。

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:

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:

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

OR else:要不然:

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

Use tr like this:像这样使用tr

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.目前, tr将删除( -d )任何不是大写和小写字母和数字的内容。

If you want to save the output to a new file, do this:如果要将输出保存到新文件,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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