Reputation: 1247
I have a line of shell script that I need to recreate the functionality of in Windows Perl?
tr -cd '[:print:]\n\r' | tr -s ' ' | sed -e 's/ $//'
The sed part is easy but not being a shell scripting expert (or perl expert for that matter) I'm having trouble recreating the functionality of the tr command in Perl.
Upvotes: 0
Views: 419
Reputation: 132886
Break down the bits to what each does and convert that to Perl:
tr -cd '[:print:]\n\r'
Take the complement (-c
) of printable characters, newlines, and carriage returns and delete them (-d
). The tr manpage tells you which each of those do.
tr -s ' '
Collapse multiple same characters (-s
) to one character.
sed -e 's/ $//'
Get rid of a trailing space.
Now just do that in whatever language you want to use. Putting it together, you might like something like:
perl -pe 's/\P{PosixPrint}//g; tr/ //s; s/ \z//;'
Note that Perl's tr does not do character classes, but I can use a complemented (\P{...}
with the uppercase P) Unicode character class to do the same thing. Perl character classes also understand the regular POSIX character classes.
Upvotes: 4