Discussion Locked

Filter special characters in file

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi folks,
I have many text files generated by a bunch of programs, from which I would like to remove special characters like ^R, ^O , etc. I would like to solve this problem via SCRIPT (I use Korn Shell). Do you know a way to do it?
Thanks in advance
Fernando

9 replies

by nikki96

You can use sed. You can pass in the name of the file to the following script. You type a control character by typing [control]+v then [control]+char (assuming your system is like mine, anyway).

If you want to do a whole list of files, you can have them all in the same directory and do a "ls" and loop through the results and send them to this script or modify this script to do so instead of reading a filename from stdin.

---------------------------------------------
#!/usr/bin/ksh

filename=$1
tmpFile="/tmp/tmp.$$"

if [[ ! -e $1 ]]; then
exit 1;

# add pipes as needed
sed 's/^R//g' $filename | sed 's/^O//g' > $tmpFile

# put finished data back
cat $tmpFile > $filename

# delete temp file
rm -f $tmpFile

-----------------------------------------------

HTH
Nikki

by volkerk

A single sed-command is sufficient, with all the special characters in a character class, like so:

sed s/[^O^P^R]//g infile >outfile
mv outfile infile

(Note that you'd have to press the key combinations Ctrl-O, not type caret and O. This alsomeans you can't enter certain combinations.)

With Perl instead of sed you could even do an in-place edit to avoid the renaming step, like this:
perl -pi -e 's/[\001-\032]' infile

Of course, what specific characters you want to replace determines whether you can use a range as in this second example or have to list each character separately. \001 is ^A, and \032 is ^Z (file-end), with \015=^M (carriage return) a good reference point in between.

by ptpage

A simpler program to use is tr. This is very similar to the solution using sed, but tr is probably faster and easier to read.

cat infile | tr -d '[\01-\37]' > tmpfile
mv tmpfile infile

This discussion is closed to new replies.