Visit Our UNIX and Linux User Community


how to check string of character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to check string of character
# 8  
Old 10-05-2007
Quote:
Originally Posted by manas_ranjan
for your satisfaction....

[:alpha:] := all letters including digit too
I always thought the [:alnum:] character class included alphabets and digits.


Quote:
Originally Posted by manas_ranjan
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
I am signing off from this thread.
# 9  
Old 10-05-2007
thanx vino,

i tried another method , check this out,

for sample in abc 123 abc123
> do
> sample_test=`echo $sample | awk '/[[:digit:]]|[[: punct:]]/ { print -1}'`
> if [ "$sample_test" = "-1" ]
> then
> echo "$sample is not valid."
> else
> echo "$sample is valid."
> fi
> done

abc is valid.
123 is not valid.
abc123 is not valid.
# 10  
Old 05-12-2008
alpha-numeric valiation

if not work properly.
please try this. ++3+3 and 1-a4-- are valid.

#! /bin/ksh
for sample in -abc 123 abc123 ++3+3 1-a4--
do
sample_test=`echo $sample | awk '/[:digit:]|[: punct:]/ { print -1}'`
if [ "$sample_test" = "-1" ]
then
echo "$sample is not valid."
else
echo "$sample is valid."
fi
done




Quote:
Originally Posted by vino
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #817
Difficulty: Medium
In CSS, E[foo="bar"] matches an E element whose "foo" attribute value is exactly equal to "bar".
True or False?

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

Featured Tech Videos