validate a pattern of numbers that are comma separated


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers validate a pattern of numbers that are comma separated
# 1  
Old 05-07-2009
validate a pattern of numbers that are comma separated

Hi,

I have a requirement wherein, I need to validate a user input of the numbers that are comma separated.
E.g . The input should be in the format 1,2,3...n (count of numbers is not known) . The user has to mention the input in this format, else it should exit from the program.
Please let me know how effectively we can handle this.
TIA
# 2  
Old 05-07-2009
You're not clear what this is for...
Are you talking about a command line utility? Or are you talking about a C program?

A simple awk script could do this if you're just validating an input file before doing something else.

I'm not the world's greatest authority on awk, as i tend to think of processing lines like I would in C, but this might help you.

#!/bin/awk

{ last=$1
for (i=2; i<NF; i++) {
if ($i < last)
exit NR
last=$i
}

This will return an exit code matching the first wrong line, or zero if everything is OK for all lines of the input.

HTH
# 3  
Old 05-07-2009
Hammer & Screwdriver Perhaps try the tr command

Code:
> echo "1,2,3,4" | tr -d "[:digit:]|,"

> echo "1,2,3,4,a" | tr -d "[:digit:]|,"
a

The first example returns nothing, while the second returns the letter 'a' -- the bad character according to my understanding of your request. The result of these commands could be set to a variable and/or simply analyzed to see if something 'fell thru' the command (and thus a bad character).
# 4  
Old 05-08-2009
Comma separated Numeric input validation

Hi,

I guess the requirement is not clear. Let me give you more description.

Firstly, I am expecting a regular expression in UNIX.

The script reads the column positions using "read col".Now when prompted to provide the values, the user should enter the required field no.'s of a file in the format 1,3,4,..n (no. of fields that a user enters is not known).
As the column no.'s should b only numbers and cannot contain any non-numeric input, the patterns like 1,a,3 or 1,4,b..or with any other special characters needs to be rejected.
I can validate the input for 2 numbers, using the regular expression
"^[0-9],[0-9]$". But, couldn't make this work for n(unknown count) of numbers.
Please let me know, how we can resolve this.

TIA
# 5  
Old 05-08-2009
if you have Python, here's an alternative
Code:
#!/usr/bin/env python
numbers = raw_input("Enter field numbers with commas (1,3,4): ")
try:
    map(int,numbers.split(","))    
except Exception,e:
    print "Error in input, not a number: ",e
else:
    print "Ok"

output:
Code:
# ./test.py
Enter field numbers with commas (1,3,4): 1,2,3
Ok
# ./test.py
Enter field numbers with commas (1,3,4): 1,2,a
Error in input, not a number:  invalid literal for int(): a

# 6  
Old 05-08-2009
Regular expression for Comma separated numbers

Thankyou.

But it would have been gr8 if we had such solution on UNIX also Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Need comma separated output

Hi, I am having the file with server names & its corresponding process, i need your help how to convert into comma separated output between server & app #cat apps.txt Server1 oracle was Server2 http webadmin Server3 tsm db2 My requirement is like below. Server1,oracle/was... (5 Replies)
Discussion started by: ksgnathan
5 Replies

4. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

5. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

6. Shell Programming and Scripting

Comma separated file

Hi all, I have the following files types: FileA: 100, 23, 33, FileB: 22, 45, 78, and i want to make File C: 100,22 23,45 33,78 any nice suggestions for making it easy. (3 Replies)
Discussion started by: hen1610
3 Replies

7. Shell Programming and Scripting

Need comma separated processing

I have a file like this OUTLN OPEN Y SCOTT OPEN N USER4 OPEN Y DBSNMP EXPIRED & LOCKED N ... (4 Replies)
Discussion started by: ilugopal
4 Replies

8. Shell Programming and Scripting

Parsing Comma separated Arguments

Hi unix guru's I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson like passing all the four names in the as the arguments and these arguments are varies between 1 to 10. How to pass these names to the shell script variable. and also i want to know the count... (4 Replies)
Discussion started by: Reddy482
4 Replies

9. Shell Programming and Scripting

Extracting the values separated by comma

Hi, I have a variable which has a list of string separated by comma. for ex , Variable=/usr/bin,/usr/smrshbin,/tmp How can i get the values between the commas separately using shell scripts.Please help me. Thanks, Padmini. (6 Replies)
Discussion started by: padmisri
6 Replies
Login or Register to Ask a Question