Determine the string is valid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine the string is valid
# 1  
Old 09-18-2012
Determine the string is valid

Dear All,

I have a string "1234567899*0#123456789#", it can be divided into:
- 1st: 1234567899 Validation: 10 digits, only 0-9
- 2nd: *0# Fixed Value
- 3rd: 123456789 Validation: 9 digits, only 0-9
- 4th: # Fixed Value

Would like to know if any 1 line statement perl, awk, sed can do this checking.

Thanks
# 2  
Old 09-18-2012
Code:
perl -e 'if($ARGV[0] =~ /\A[0-9]{10}\*0#[0-9]{9}#\z/){print "Valid string\n"}else{print "Invalid string\n"}' '1234567899*0#123456789#'

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-18-2012
Thanks elixir, it works perfect.
regarding the \A, and \z, would u please guide me those two meaning.
# 4  
Old 09-18-2012
Both of these are anchors which force a match to be performed at that particular position. \A is the beginning-of-string anchor (absolute) and \z is the absolute end-of-string anchor.
# 5  
Old 09-18-2012
Thanks elixir, learnt a lot. Smilie
# 6  
Old 09-18-2012
Code:
[ `echo '1234567899*0#123456789#' | egrep '^[0-9]{10}\*0#[0-9]{9}#'` ] && echo good || echo bad


Last edited by Franklin52; 09-18-2012 at 05:42 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 Replies

2. Shell Programming and Scripting

Check if a string is a valid timestamp in UNIX.

Hi all, I have date and time value in a string, I want to check if it is a valid date and time. Need help on this. Thanks (7 Replies)
Discussion started by: Pratiksha Mehra
7 Replies

3. Shell Programming and Scripting

Fetch Valid String

Hi All, How to fetch the particular row based on my search command. For example Source Column Column A Column B 001,56123456 002,57123456 003,123456 Expected Output: 003,123456 To get this output I tried below mentioned function grep '123456' filename.txt (4 Replies)
Discussion started by: suresh_target
4 Replies

4. Shell Programming and Scripting

Using awk to determine if field value is valid

Hi Forum. I tried to search the forum posts for an answer but I haven't been able to do so for what I'm trying to accomplish. I have the following source file: 11936385~TFSA|11936385|4431|3401458067|10/09/1982|25.00|IBSBONUS|3200|||||CASH| 3401458067|1005|... (3 Replies)
Discussion started by: pchang
3 Replies

5. Shell Programming and Scripting

Using filename to determine a prefix that needs to be added to string column on file?

Possible filenames: CDD_Whatever.txt DDD_Whatever.txt If the file prefix = CDD, I'd like to prefix every person ID (second column in my examples below) on the file with "c-" If the file prefix = DDD, I'd like to prefix ever person ID with "d-" Input: Desired Output: Any help... (2 Replies)
Discussion started by: lrluis
2 Replies

6. Shell Programming and Scripting

Whether a string is a valid unix command

How to find a string which is entered in command promt is a valid unix command or not?. Thanks in advance ~Saravana (2 Replies)
Discussion started by: tsaravanan
2 Replies

7. Shell Programming and Scripting

Need to get next valid date from date string

Hi, I am passing date string of format 'YYYYMMDD' to a ksh script. Will I be able to get next valid date from the passed in string. Example I pass '20100228' to the shell script, Is there a reverse date command to get '20100301' .i.e to convert '20100228' to date and get next date.... (5 Replies)
Discussion started by: bittoo
5 Replies

8. Shell Programming and Scripting

[ksh, awk] determine values from rather complcated string

Hi there, I tried and tried, but cannot solve this myself. I have this string: enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffPurge.purgelistAlarmIds.0 = {alarmId=63868|friendlyName=Dortmund/r01sr1sl01/port#06-#01-Vc12|probableCause=Server Signal... (3 Replies)
Discussion started by: ejdv
3 Replies

9. Shell Programming and Scripting

Determine string in Perl.

Hi, I have a little Perl question. I need to determine the last word in the following string: h t t p://abc.def.com/hijklm The output should be the string hijklm. h t t p is of course http. The string between the slashes always differs. The string after the last slash always differs.... (4 Replies)
Discussion started by: ejdv
4 Replies

10. Shell Programming and Scripting

$A is a number / any other string? How to determine ?

I have a variable (say $A) and while passing it gets either a number or some other string. Now how can test (with if else) whether the variable is just a ne or something else ? Thanks a lot to all in advance C Saha (2 Replies)
Discussion started by: csaha
2 Replies
Login or Register to Ask a Question