LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-26-2008, 08:14 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Question bash command for removing special characters from string


hi all

I am want to remove these special characters ( "." "-" "_" ) from a string (string is username).
How can I solve this problem ?


Thanks & Regards
KKPal
 
Old 05-26-2008, 08:35 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
You can use any of 'sed', 'awk', and numerous others. But 'sed' and 'awk' exist on most if not all *NIX systems. For more information, read the manuals, look at the scripts on the system, and play around a bit.
 
Old 05-26-2008, 08:36 AM   #3
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
hi all

I solved it. Solution is

echo "$string" | sed 's/_//' | sed 's/-//' | sed 's/\.//'

Its working but it is not proper solution.
How can I do it with single sed.


Thanks & Regards
KKPal
 
Old 05-26-2008, 08:41 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Is this what you are looking for?

echo $string | sed 's/[\._-]//g'

Hope this helps.
 
Old 05-26-2008, 09:09 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
This is even shorter:
Code:
echo ${string//[-._]/}
 
Old 05-26-2008, 09:14 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

@jschiwal: And more elegant......
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing file with special characters rsashok Linux - General 4 02-09-2010 05:08 PM
Removing special characters from filenames PlymWS Linux - Software 1 08-09-2007 06:11 AM
inserting special characters into mysql with bash script ihopeto Linux - Newbie 1 12-05-2006 01:46 PM
Bash Renaming (removing a string from filenames) FreeDoughnut Programming 9 07-20-2006 10:54 PM
bash and filenames with special characters CoolAJ86 Programming 2 03-09-2005 03:50 PM


All times are GMT -5. The time now is 03:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration