Sponsored Content
Top Forums Shell Programming and Scripting ksh check for non printable characters in a string Post 302935080 by Don Cragun on Thursday 12th of February 2015 03:01:15 PM
Old 02-12-2015
Quote:
Originally Posted by dips_ag
Hi Don,

I think there is some problem with this syntax.

test.ksh
Code:
if [ "${TEXT#*[![:alnum:] .,;:'"/\()_+=~@&*-]}" = "$TEXT" ];then echo 'no non-printables found';else echo 'non-printable found';fi

Code:
 
+ TEXT='This is a sample text with supposedly non-printable character^Y.'
./test.ksh: line 5: syntax error at line 47: `'' unmatched

Then I introduced a \ before '

Code:
if [ "${TEXT#*[![:alnum:] .,;:\'"/\()_+=~@&*-]}" = "$TEXT" ];then echo 'no non-printables found';else echo 'non-printable found';fi

then the error was -
Code:
+ TEXT='This is a sample text with supposedly non-printable character^Y.'
./test.ksh: line 5: syntax error at line 47: `{' unmatched

then I I introduced a \ before "
Code:
if [ "${TEXT#*[![:alnum:] .,;:\'\"/\()_+=~@&*-]}" = "$TEXT" ];then echo 'no non-printables found';else echo 'non-printable found';fi

error was -
Code:
 
+ TEXT='This is a sample text with supposedly non-printable character^Y.'
./test.ksh: line 5: syntax error at line 47: `)' unexpected

Then finally I introduced \ before all the shell special characters -
Code:
 
echo ${TEXT#*[![:alnum:] .,;:\'\"/\(\)\_+=~@&\*-]}

which resulted into
Code:
Y.

But I think that's wrong because it should have resulted ^ as that's the only punctuation mark not included in the list?! Smilie

-dips
I apologize for not trying this out before I posted it.

In a BRE or an ERE special RE characters lose their special meaning when inside a bracket expression, but that is not true in a shell pattern matching expression. I'm glad you were able to figure out what was needed to make it work for you. Even here, the underscore and the asterisk do not need to be escaped.

The output from:
Code:
echo ${TEXT#*[![:alnum:] .,;:\'\"/\(\)_+=~@&*-]}

is correct. The * matched (and discarded):
Code:
This is a sample text with supposedly non-printable character

and the:
Code:
[![:alnum:] .,;:\'\"/\(\)\_+=~@&\*-]

matched and discarded the ^ just leaving
Code:
Y.

in that expansion of $TEXT. The whole point of that expansion is to find a remove one character that is not in the set of characters that you are declaring to be "non-printable" with the non-matching bracket expression. The the if statement comparing the original string and the original string with a non-printable character removed compare equal if and only there are no non-printable characters in the string.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Best way to search files for non-printable characters?

I need to check ftp'd incoming files for characters that are not alphanumeric,<tab>, <cr>, or <lf> characters. Each file would have 10-20,000 line with up to 3,000 characters per line. Should I use awk, sed, or grep and what would the command look like to do such a search? Thanks much to anyone... (2 Replies)
Discussion started by: jvander
2 Replies

2. UNIX for Dummies Questions & Answers

Ksh Checking if string has 2 characters and does not contain digits?

How could I check if a string variable contains at least (or only) 2 characters, and check and make sure that the string does not contain any numeric digits?...I need to know how to do this as simple as possible. and I am using the Ksh shell. Thanks. (1 Reply)
Discussion started by: developncode
1 Replies

3. Shell Programming and Scripting

grep non printable characters

Sometimes obvious things... are not so obvious. I always thought that it was possible to grep non printable characters but not with my GNU grep (5.2.1) version. printf "Hello\tWorld" | grep -l '\t' printf "Hello\tWorld" | grep -l '\x09' printf "Hello\tWorld" | grep -l '\x{09}' None of them... (3 Replies)
Discussion started by: ripat
3 Replies

4. UNIX for Dummies Questions & Answers

delete non printable characters from file

i have a file which contains non printable characters like enter,escape etc i want to delete them from the file (2 Replies)
Discussion started by: alokjyotibal
2 Replies

5. HP-UX

Non-printable characters

I have been using OKI data Microline printers; models 590 and 591 to print a bar code using the following escape sequence: \E^PA^H^C00^D^C^A^A^A\E^PB^H The escape sequence is stored in a unix file which is edited using vi. Now, we are considering Microline printer model 395C and the bar code... (3 Replies)
Discussion started by: Joy Conner
3 Replies

6. Shell Programming and Scripting

Check whether there is a non printable character in the unix variables

cp $l_options $srcdirfile $destdirfile If i want to check whether there is a non printable character in the variables $l_options $srcdirfile $destdirfile how it can be done? (2 Replies)
Discussion started by: lalitpct
2 Replies

7. Shell Programming and Scripting

Removing Non-printable characters in unix file

Hi, We have a non printable character "®" in our file , we want to remove this character, we tried tr -dc '' < oldfile> newfile but this command is removing all new line entries along with the non printable character and all the records are coming in one line(it is changing the format of the... (2 Replies)
Discussion started by: pyaranoid
2 Replies

8. UNIX for Dummies Questions & Answers

removing non printable characters

Hi, in a file, i have records as below: 123|62|absnb|267629 123|267|28728|uiuip 123|567|26761|2676 i want to remove the non printable characters after the end of each record. I guess there are certain charcters but not visible. i don't know what character that is exactly. I used... (2 Replies)
Discussion started by: pandeesh
2 Replies

9. Shell Programming and Scripting

Unable to grep control/non printable characters

Unable to grep: Able to grep: (11 Replies)
Discussion started by: proactiveaditya
11 Replies

10. Shell Programming and Scripting

Redirecting records with non-printable characters

Hi, I have a huge file (50 Mil rows) which has certain non-printable ASCII characters in it. I am cleaning the file by deleting those characters using the following command - tr -cd '\11\12\15\40-\176' < unclean_file > clean_file Please note that I am excluding the following - tab,... (6 Replies)
Discussion started by: rishigc
6 Replies
All times are GMT -4. The time now is 09:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy