1748051 Members
5171 Online
108758 Solutions
New Discussion юеВ

Re: tr and :print option

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

tr and :print option

As found on another thread here, I am looking to strip out unprintable characters from a file. So I send my file through the following command:

tr -d [^:print]

Seems to make sense to me; delete all characters that are not in the printable class.

(There is no specific info in the man page for tr about :print, but this seems to be what it says).

When I run the following data through the command:

man tr
vi find_new_histfile_lines
cd /admin/scripts/
vi find_new_histfile_lines
sh -x find_new_histfile_lines

I get the following output:

ma
v fd_ew_hsfle_les
cd /adm/scs/
v fd_ew_hsfle_les
sh -x fd_ew_hsfle_les

Does this make any sense at all? It seems like certain characters (e.g., "n", "t") are being treated as unprintable.

Scott
8 REPLIES 8
Rodney Hills
Honored Contributor

Re: tr and :print option

I don't theink you can use "^" in a class. Try-
tr -d [:graph:]

"graph" is non-printable characters. Also in your sample you were missing a colon. The class would be [:print:].

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: tr and :print option

In this context you are actually telling tr to remove all ':','p','r','i','n', and 't' characters.

I would do it like this: Note that printables include ' ' through '~' (octal 040 - 176) and you probably don't want to throw away LF's (octal 012) as well. Not that the 'c' tr option complements the string so we don't need no stinkin' '^'.

cat myfile | tr -cd "[\040-\176][\012]" > newfile

If your want to keep formfeeds and CR's as well the append their octal equivalents to the string as well (inside brackets).
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: tr and :print option

Instead of using tr, you could also use "strings" to not print unprintables.

HTH

-- Rod Hills
There be dragons...
Scott Lindstrom_2
Regular Advisor

Re: tr and :print option

A.Clay,

I was basing this on another post and what I see in the man page for "tr":

[:class:]or [[:class:]] Stands for all the characters belonging to the
defined character class, as defined by the current
setting of LC_CTYPE locale category. The following
character class names will be accepted when
specified in string1: alnum, alpha, blank, cntrl.
digit, graph, lower, print, punct, space, upper,
or xdigit

I have tried with and without the "^", and with "[" and "[[". So while I understand it seems to be omitting these characters, I wonder how I can make the class of ":print" work.
Scott Lindstrom_2
Regular Advisor

Re: tr and :print option

Rodney - I see the missing trailing ":" you mention. So I tried this:

tr -d [:print:]

and this:

tr -d [^:print:]

The first gives me the same results, the second gives me all blanks.

Scott

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: tr and :print option

Okay, if you insist upon the class specifier (which I find less readable and less intuitive) then

cat myfile | tr -Acs "[[:print:]][\012]" > newfile
If it ain't broke, I can fix that.
Scott Lindstrom_2
Regular Advisor

Re: tr and :print option

Rodney -

I was origianlly using strings, but I saw that one character commands (like "w") were being stripped out by strings. So that's why I'm looking at tr.

Scott
A. Clay Stephenson
Acclaimed Contributor

Re: tr and :print option

Ooops, I mistyped:

cat myfile | tr -Acs "[[:print:]][\012]" > newfile

should, of course be:

cat myfile | tr -Acd "[[:print:]][\012]" > newfile
If it ain't broke, I can fix that.