0

I'm having trouble on executing sed command. I would like to know first if sed command is really working on ksh script. I'm using putty as my tool to execute ksh script.

If it really works, my command that I am using is sed -e [^a-zA-Z0-9] <file>.

I actually do this command for grep, grep [^a-zA-Z0-9] <file>.

My goal is to get all special characters in my text file and return it to a variable or another file. I'm searching for this one for this site and all other site but I never found any that really works for me.

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
JMM
  • 1

2 Answers2

1

Yours is not a valid sed script. The problem is exacerbated by the lack of quoting.

Without quotes, the shell attempts to expand the expression [^a-zA-Z0-9] to a list of matching file names. If you have files named, say, , and ? in the current directory, the glob will be expanded to grep , ? <file> even before grep runs (depending also on your shell's settings -- with shopt -s nullglob the pattern will disappear if there are no matches, for example).

The syntax of sed is different. To match a regular expression with sed, use

sed '/[^a-zA-Z0-9]/' <file>

The slash is configurable; for example, if you have a regular expression which contains slashes, you can use a different separator by prefixing it with a backslash:

sed '\#[^a-zA-Z0-9/]#' <file>

If you want to extract just the characters which match, try grep -o -- and again, take care to quote your variables properly.

chars=$(grep -o '[^a-zA-Z0-9]' <file>)
echo "$chars"  #  double quotes are *absolutely* *crucial*

Without the double quotes, again, the shell would attempt to expand any * in chars to a list of files in the current directory.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Quoting is explained in more detail at http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Oct 18 '16 at 05:46
0

sed is a binary utility external to your shell. You can see where it is with

$ which sed
/usr/bin/sed

Your stated goal is to get all of the "special" characters from a text file using sed and/or grep, where a special character is any that is not in the sets a-z, A-Z, or 0-9. Using sed is easiest:

$ sed -e 's/[a-zA-Z0-9]//g' file

This will replace all non-special characters with an empty character, effectively removing them from the file. Example using /etc/passwd as input will output something similar to this:

$ sed -e 's/[a-zA-Z0-9]//g' /etc/passwd
:::::/://
:::::/://
:::::/://
::::://://
:::::///://
:::::/://
:::::/://
:::::/://
:::::///://
:::::/://
::::://://
:::: ://://
:::::/://
:::::///://
-::::  :/://
-::::  :/://
-:::: :/://
--::::  :/://

For grep you can use the -o option to output the matching values:

$ grep -o '[^a-zA-Z0-9]' /etc/passwd

which will produce similar output, however, each matching character appears on a new line.

Another option is tr:

$ tr -d '[:alnum:]' </etc/passwd

which will produce the same output as the previous sed command.


In all three cases you can capture the stdout of the command like this:

$ specials=$(tr -d '[:alnum:]' </etc/passwd)
$ echo $specials
:::::/:// :::::/:// :::::/:// ::::://:// :::::///:// :::::/:// :::::/:// :::::/:// :::::///:// :::::/:// ::::://:// :::: ://:// :::::/:// :::::///:// -:::: :/:// -:::: :/:// -:::: :/:// --:::: :/:// :::: :/:// :::: :/:// :::: :///:// :::: :///:// ::::://:// :::: ://:// :::::/:// :::: :///:// :::::///:// :::: /- :///-:// :::::///:// :::: :///:// :::: :/:// :::::///:// ::::://:// -:::: :/:// :::: :/:// :::: ://:// :::: :/:// :::: :///:// :::: :///:// --::::://--/:// -:::: :/:// ::::- :///:// :::: ://:// :::::/:// ::::://:// ::::://:// :::: ://:// :::: :///:// :::: :///:// :::: :///://

Using back quotes also works:

$ specials=`tr -d '[:alnum:]' </etc/passwd`
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Not quoting the variable like `echo "$specials"` can have surprising and hard-to-debug effects as outlined in my earlier answer. – tripleee Oct 18 '16 at 14:51
  • Thanks! :) this one works for me grep -o '[^a-zA-Z0-9]'. – JMM Oct 19 '16 at 00:31
  • @JMM: no problem. All 3 options work. If you are happy with the answer you could consider accepting it. – mhawke Oct 19 '16 at 04:39
  • @mhawke what do you mean accepting it? I apologize I'm just new to this site. Was it anything that I can click like buttons? – JMM Oct 19 '16 at 23:10