4

I want the program can remove punctuation which read from the standard input My code is:

echo $* | tr -d '[:punct:]'

It can handle some simple situations but when I type input in terminal (like: whatever ad!":)

when the sentence within continuing several punctuation, the terminal will reflect the result: -bash: !": event not found

Can anyone give help?

Himanshu
  • 4,327
  • 16
  • 31
  • 39
JAC
  • 53
  • 1
  • 1
  • 5
  • you can also use sed instead of `tr` like `echo 'Whatever ad!":' | sed 's/[[:punct:]]//g''` – Avinash Raj May 07 '14 at 07:11
  • $* is the string parameter that I type it in terminal. The file to edit shell script named stripchars and commands and pipe are edited in this file, and I run it like ./stripchars abc abc can be instead of other strings. So i don't want string is to be edited inside the file. Do you have any other ideas to solve the problem if the string includes continuous punctuation and how to treat ! continue with another punctuation as normal char – JAC May 07 '14 at 07:32

2 Answers2

4

single quotes should be used to avoid expansion, e.g.

echo 'whatever ad!' | tr -d '[:punct:]'

under a bash shell it prints out

whatever ad

and if you want to use a variable

BUFF='whatever ?_-!!!!ad!'; echo "$BUFF" | tr -d '[:punct:]'

EDIT 1

this is a complete script following your request

#!/bin/sh
functionStripAndPrint()
{
  echo "$@" | tr -d '[:punct:]'
}

functionStripAndPrint "$@"

assuming that this script is stored in the stripchars.sh file, you can invoke it like so

./stripchars.sh 'das !adsa _sda ssad-'

and it will print

das adsa sda ssad

EDIT 2

you can work around the interpretation of some of the special characters with set, for example

set +H

deactivates the H option which is linked to the ! symbol, so now ! is just an exclamation mark with no special meaning. You can then simplify your invocation a little bit

./stripchars.sh sdfsa!fdsaf?\'

as you can see the only problem at this point is the ' that still needs to be escaped.

If you want to re-enable the H you do

set -H

set is handy to modify the behaviour of your shell, I don't know if it's worth in your case, the shell is good and handy for some basic stuff, but I don't know if this will fit your needs, you know better, take a look at set and see if it's enough.

user2485710
  • 9,451
  • 13
  • 58
  • 102
  • $* is the string parameter that I type it in terminal. The file to edit shell script named stripchars and commands and pipe are edited in this file, and I run it like ./stripchars abc abc can be instead of other strings. So i don't want string is to be edited inside the file. Do you have any other ideas to solve the problem if the string includes continuous punctuation and how to treat ! as normal char – JAC May 07 '14 at 07:29
  • @JAC see if the edit fits your needs . – user2485710 May 07 '14 at 07:42
  • thank you for your help. but did you try if the end of the string is !" or ?" , it also shows -bash: even not found, do you have any method can solve that problem – JAC May 07 '14 at 07:54
  • @JAC I'm under Ubuntu with a bash and it works with any character, I can't see any particular problem with it, how do you invoke it ? Please add more details . – user2485710 May 07 '14 at 07:56
  • can you do not use single quotes for the string, just type string without quotes, or do you know how to convert it to let the string i typed without quotes be quoted – JAC May 07 '14 at 08:05
  • @JAC if your invocation is `'sdfsa!fdsaf?''` your problem is the second `'`, because it needs to be escaped and if not, it will redefine your string closing it before the third `'`; this will also cause the third `'` to be read as a special character and this is what is triggering the message that you see. The invocation with `./stripchars.sh 'sdfsa!fdsaf?'` is totally fine, you can't use `'` like that because it's a string delimiter for the shell. If you need some advanced string manipulation features, and you find that the shell it's not enough, I'll suggest to switch to python 2.x . – user2485710 May 07 '14 at 08:15
  • @JAC I have added 1 more option, but `'` and `"` are builtin delimiters for the bash, I don't think you can work around those 2. – user2485710 May 07 '14 at 08:37
2

As you probably know, bash uses ! to get commands from the history of commands. When you type

echo Whatever ad!":

it tries to retrieve the command from its command history by using !":. Since it does not find any command using that, it prints the message

bash: !": event not found

You can pass those special characters to bash by (1) using single quote to let special characters be treated like normal characters, or (2) escaping the special characters.

echo 'Whatever ad!":'  | tr -d '[:punct:]' 
echo Whatever ad\!\":  | tr -d '[:punct:]'
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • text(Whatever ad!":) is an parameter that I typed in terminal, and the command to remove punctuation is edited in a file. I want to let the program can solve the situation when I type anything which include continuous punctuation like(!":") in terminal directly as standard input not to edit the text inside of the shell script file. I have an idea about to convert the parameter which is a string to normal characters, but I don't know how to convert. – JAC May 07 '14 at 07:22