The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove directory that has special Characters datherriault UNIX for Dummies Questions & Answers 12 01-14-2009 05:53 PM
remove special and unicode characters shantanuo UNIX for Dummies Questions & Answers 1 12-05-2008 09:16 AM
Remove lasts characters from a string chriss_58 Shell Programming and Scripting 6 11-28-2008 06:07 AM
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 07:13 PM
Add string after another string with special characters heliode Shell Programming and Scripting 2 03-21-2008 08:06 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Question Remove special characters from string

Hi there,
I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example:
Code:
$ myscript "My name's Santiago. What's yours?" "atu"
My nme's Snigo. Wh's yors?
I wrote the following code:
Code:
$ cat myscript
ans=$1
for (( i=0; i<${#2}; i++ )); do
    ans=${ans//${2:$i:1}/}
done
echo "$ans"
But it doesn't work with the following special characters: #%*?\
Is there any simplier way to do that?
How can I make it work with all the characters?

Thanks in advance
Santiago
  #2 (permalink)  
Old 12-04-2008
Wolfclaw Wolfclaw is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Try passing the special characters with a \ in front.
On the other hand why not simply use regular expressions with sed or perl etc. ?
  #3 (permalink)  
Old 12-04-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Or simply with tr. Check the -d option in the man page.

Regards
  #4 (permalink)  
Old 12-04-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Quote:
Originally Posted by Wolfclaw View Post
Try passing the special characters with a \ in front.
On the other hand why not simply use regular expressions with sed or perl etc. ?
That's exactly what I was asking! I'm sure there's a simpler way of doing that... But how?
I cannot pass the special characters with a backslash because my script reads the second argument character by character. So if $2 is "\#", the script will first try to replace \ (and it fails) then try to replace # (and it fails too).
Any other idea?
  #5 (permalink)  
Old 12-04-2008
Wolfclaw Wolfclaw is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
for some reason its working with single quotes in the command line arguments. I dont know why this is though *shrugs*
  #6 (permalink)  
Old 12-04-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
And what's wrong with Franklin52's solution?

This is with bash:

Code:
$ ./s "My name's Santiago. What's yours?" 'atu'
My nme's Snigo. Wh's yors?
$ ./s "My# name%'s ?Santiago.\ What/'s yours?" '#%?/\'
My name's Santiago. What's yours
$ cat s
#! /bin/bash

tr 2>&- -d "$2"<<<"$1"
  #7 (permalink)  
Old 12-04-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Quote:
Originally Posted by Franklin52 View Post
Or simply with tr. Check the -d option in the man page.
Great! The only character that do not work is backslah but here my new solution:
Code:
~$ cat myscript
echo "$1" | tr -d "${2//\\/\\\\}"
~$ myscript 'weird string !"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' ' !"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~'
            <---------------------- $1 ----------------------> <---------------- $2 ---------------->
weirdstring
~$
Thanks a lot
Sponsored Links
Reply

Bookmarks

Tags
shell script, shell scripting, str, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0