special characters in IF TEST


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users special characters in IF TEST
# 1  
Old 02-24-2012
special characters in IF TEST

I'm using Korn shell. I'm doing an IF TEST for lots of characters and don't know how to also check for single quote and parentheses and slash. I'm reading a file and some records have garbage characters in them. The following works, but how do I add single quote, parentheses and slash to the IF TEST:
Code:
# put end bracket first
   if [[ $LINE = +([]a-z A-Z [+|. _~ 0-9-]) ]]
      then echo good
      else  echo "contains bad"
   fi

Thank you

Last edited by radoulov; 02-24-2012 at 04:15 PM.. Reason: Code tags!
# 2  
Old 02-24-2012
It certainly doesn't work for me. No matter what I feed into it, I get 'contains bad'.

What exactly are you trying to do? Maybe it would be easier to match the characters you do want than the characters you don't.
# 3  
Old 02-24-2012
Something like this may work:

Code:
#!/opt/ast/bin/ksh

print ${.sh.version}

set -- ')' ' ' "'" 'abc' '\' 42 '\)ab('

for w; do
  if [[ $w = *[\'\\\)\(]* ]]; then 
    print "match found for ->$w<-"
  else  
    print "no match  found for ->$w<-"
  fi
done

I get the following output:

Code:
~/t $ ./s                                                                                                          
Version JM 93u 2011-02-08
match found for ->)<-
no match  found for -> <-
match found for ->'<-
no match  found for ->abc<-
match found for ->\<-
no match  found for ->42<-
match found for ->\)ab(<-

You may consider using case for portability.
# 4  
Old 02-24-2012
The script sort of works using ksh.
For those who are interested it's in this bit of the ksh (and Posix Shell) manual:
Code:
man ksh|grep "\+("

... and to find it when in "man ksh" with common man PAGER settings
/\+\(

Imho. If the file contains Shell special characters you will need to filter using an external program such as "tr" or "awk" or even "sed".
If all you want to do is to remove the bad characters, then this is relatively easy.

What do you want to do with a good record?
What do you want to do with a bad record?

Last edited by methyl; 02-24-2012 at 07:23 PM.. Reason: layout and addenda
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Look for substrings with special characters

Hello gurus, I have a lookup table cat tmp1 \\\erw``~ 1 ^774574574565665f\] 2 ()42543^ and I`m trying to compare a bunch of strings such that, either the lookup table column 1, or the string to be looked up are substrings of each other (and return the second lookup column if yes). ... (2 Replies)
Discussion started by: sheetalk
2 Replies

2. UNIX for Advanced & Expert Users

Filter special characters

I have a text file that has these special characters. ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^E How would I go about removing them? They come from a c program. I have to use a for loop like this or my outputs gets messed it. I can't use the %s option or my output won't be in the way I need it.... (2 Replies)
Discussion started by: cokedude
2 Replies

3. Shell Programming and Scripting

Test command with special character not work

Hi all, Case 1 : A=88^M && echo "PASS" Result: PASS Case 2: A=88 && echo "PASS" Result: PASS I would like to know why Case 1 and Case 2 got the same result? What make ^M ignored ? Thanks in advance. (6 Replies)
Discussion started by: montor
6 Replies

4. Shell Programming and Scripting

A test command parameter is not valid, when special characters are tried to match

Hi I have a scenario where hyphen(-) from file should be ignored I used the following code if && ; then if ; then pow=$LINE echo $pow > history.txt flag=1 fi fi I get the following output ./valid.sh: -: 0403-012 A test... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

5. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

6. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

7. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

8. Shell Programming and Scripting

remove special characters

hello all I am writing a perl code and i wish to remove the special characters for text. I wish to remove all extended ascii characters. If the list of special characters is huge, how can i do this using substitute command s/specialcharacters/null/g I really want to code like... (3 Replies)
Discussion started by: vasuarjula
3 Replies

9. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

10. UNIX for Dummies Questions & Answers

special characters

I have one file which is named ^? ( the DEL character ) I'd like to know how to rename or copy the file by using its i-node number TYIA (2 Replies)
Discussion started by: nawnaw
2 Replies
Login or Register to Ask a Question