how to check string of character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to check string of character
# 1  
Old 10-05-2007
how to check string of character

how can i check whether variable contains only character from a-z or A-Z....if my variable contains any alpha numeric, numeric or any character with some special one i.e. *%&@! etcetera etcetera....then it should show me please enter only characters......

Let my variable
var1="abc77}|"

then it should show me "please enter valid character"
# 2  
Old 10-05-2007
# 3  
Old 10-05-2007
hey vino..thanx a lot.......
but neither of the commands are working perfectly for me........in bash
thing is i should allow only those variable which are string...i.e. contains only character....
# 4  
Old 10-05-2007
Quote:
Originally Posted by manas_ranjan
hey vino..thanx a lot.......
but neither of the commands are working perfectly for me........in bash
thing is i should allow only those variable which are string...i.e. contains only character....
Those links were meant to be a starting point. I think you understood the code snippets in each. Try to write one based on your understanding. And the when you face an issue, post the code you have so that we can help you better.
# 5  
Old 10-05-2007
hey....vino.....
if i could able to solve on my own..i sudn't post it here....
so help me out...i tried each post provided in replied to this post by you.....but fortunately ya unfortunately...i didn't find a single method..which will help me out in bash .......if you could help me , then please.....

while implying this one....+([a-zA-Z]) , ?(+|-)+([0-9]) and *([0-9]|[a-zA-Z])*...i am getting
-bash: syntax error in conditional expression: unexpected token `('
-bash: syntax error near `+(['


any idea......????
# 6  
Old 10-05-2007
Quote:
Originally Posted by manas_ranjan
hey....vino.....
if i could able to solve on my own..i sudn't post it here....
When you face an issue, you show us what you tried, the error that you got and then we would be in a better shape to help you out.

Anyway, this should get you started.

Code:
[/tmp]$ cat try.sh
#! /bin/sh

for sample in abc 123 abc123
do
    if [[ $sample == *[[:alpha:]]* ]] ; then
        echo $sample
    fi;
done
    
[/tmp]$ ./try.sh
abc
abc123
[/tmp]$

I leave the rest to you as an exercise. Look under the section 'Pattern Matching' in the man pages of bash for other character classes.

Last edited by vino; 10-05-2007 at 09:50 AM.. Reason: revised.
# 7  
Old 10-05-2007
hey.......i did the same thing before posting this let me clear one more time...not successful.......only i sud display abc......
is there any way to find only alphabets.....

for your satisfaction....

[:alpha:] := all letters including digit too

so i request you please go thru the query/questions.....where i clearly mentioned i need only character sets......even though my variable has alphanumeric/numeric/ i sud say please enter valid characters
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

How to check for special character in a value

Hi, I have a variable and to it always alphanumeric value will be assigned. If the value has any special characters in it then in the if statement it should exit like below if (value has any speacial character) then exit else .... fi can any one suggest how to acheive this? (4 Replies)
Discussion started by: lavnayas
4 Replies

4. Shell Programming and Scripting

check number of character

hi, I would like to calculate number of character for a number, for exemple : 1200 --> there are 4 characters , 120001 -> 5 characters (4 Replies)
Discussion started by: francis_tom
4 Replies

5. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

6. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

7. Shell Programming and Scripting

bash script to check the first character in string

Hello would appreciate if somebody can post a bash script that checks if the first character of the given string is equal to, say, "a" thnx in advance (2 Replies)
Discussion started by: ole111
2 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Programming

converting character string to hex string

HI Hi I have a character string which contains some special characters and I need it to display as a hex string. For example, the sample i/p string: ×¥ïA Å gïÛý and the o/p should be : D7A5EF4100C5010067EFDBFD Any pointers or sample code pls. (5 Replies)
Discussion started by: axes
5 Replies

10. UNIX for Dummies Questions & Answers

check for the first character to be blank

I'm having a problem when the first line or first character of a file is blank. I need to get rid of both of them when they occur but don't want to delete the line. Does anyone have any suggestions? (7 Replies)
Discussion started by: anthreedhr
7 Replies
Login or Register to Ask a Question