How to exclude all speacial characters using regex?
Hi Folks,
I need to exclude special characters from file and only include [a-zA-Z0-9] . - In-fact I am just including domain names and exclude all special characters. I am not able achieve the same. ~`!@#$%^&*()_+={}[]\|;:'"<,>/? Can someone please help? |
which language is it? do you have any written code already?
|
|
Quote:
Any hint? |
My sample text would be
example.com test.com test123.com 123test.ocm calid-domain.com test-test.net !def @fsf dafsrf# fffgg$.net %rrt.com ^testcom asddf&.net as* ( ) _ + = \ ; : ' " < , > ? / |
Quote:
https://www.linuxquestions.org/quest...es-4175657403/ https://www.linuxquestions.org/quest...nd-4175656948/ https://www.linuxquestions.org/quest...rs-4175655180/ https://www.linuxquestions.org/quest...ng-4175648204/ https://www.linuxquestions.org/quest...es-4175641557/ https://www.linuxquestions.org/quest...pt-4175635666/ https://www.linuxquestions.org/quest...pt-4175616729/ Show your own efforts when posting, and do basic research. After three years, you should have SOME scripting/research skills. Putting "bash regex strip out anything but letters and numbers" into Google pulls up a LOT of 'hints'. You've been told many times to post things in CODE tags, but don't seem to follow that advice either. The [:alnum:] is alpha-numeric. |
Quote:
Will definitely ensure to follow the code tags. |
You need to escape certain characters inside the RegEx:
Code:
while read -r line;do I will leave matching the remaining characters as an excercise. PS: You can also achieve this by using [:alnum:] by TB0ne but it has also a pitfall. I think, however, that doing it the "hard" way is more educational in the long run since you can learn how to handle certain characters in a RegEx. |
Code:
'[!@#%%$^*()_+=\;:,"<>?/]' |
Read post #8 again.
|
Quote:
Quote:
|
Better name the printable characters, and use the complement of it, either with tr and -c option, or with a negating ^ in a charset in a RE:
Code:
tr -dc '.a-zA-Z0-9\n-' < samplefile |
just a quick test of that one loop.
Code:
#!/bin/bash Code:
[][()\'\"~!\`@/?\>\<\\] Code:
[userx@arcomeo testdir]$ ./stripme testfile |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 11:42 PM. |