Unix grep/test command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix grep/test command
# 1  
Old 10-08-2007
Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed:

Code:

# Check to see if the 8 characters are all numbers
# If not show error essage
# And prompt user for more input

         echo $char | grep -q '^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$'  
if 	[ $? -ne 0 ]
then
         echo "You have entered non-numeric values.  Please type in the form of YYYYMMDD"
	read char
        continue


Is there a simpler way to write this command without using the [0-9] value for each field? Any help would be appreciated
# 2  
Old 10-09-2007
On Solaris you can use:

Code:
echo "12345678" | /usr/xpg4/bin/grep -Eq "^[0-9]{8}$"

In any case, you must use a tool which supports extended regular expressions to use the above regex.
# 3  
Old 05-07-2009
Comma delimited instead of space delimited

Hi,

What if the numbers are comma separated and the no.of occurences of the pattern is not known.
E.g. 1,2,3,4,...n

TIA
# 4  
Old 05-07-2009
@12345 If i understood your question correctly, then the regex is:
Code:
grep -E "^[0-9,]{3,}"

# 5  
Old 05-08-2009
regular expression for Comma separated numbers

Hi,

Thanks for your reply. I just want to make this requirement more clear.
I read values to a variable, using "read var".I am now validating the user input. The user should input the values in the format 1,3,6 (can enter any numeric values upto n).
I have tried this for 2 numbers. This is the command that I had used.
read col
1,2 ----> input provided by user
echo $col | grep -w "^[0-9],[0-9]$"
return code is successful.
If it contains any other input like 1,a or a,1 or a,b it returns a code of 1.

This code works fine for 2 numbers, but I want to make this work for 'n' numbers.
I had tried the solution provided by you, but -E command is not recognised by my script.
TIA
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using grep with test and without using [[ ]]

As an exercise, I'm trying to re-write this code without the compound square brackets, using grep and test. Need to know what to do about the "equal-tilde". #!/bin/bash # test-integer2: evaluate the value of an integer. INT=-5 if +$ ]]; then if ; then echo "INT is zero." else if ; then... (17 Replies)
Discussion started by: Xubuntu56
17 Replies

2. UNIX for Beginners Questions & Answers

Help with UNIX test and wc Command

I want to xheck if a file exists that uses wildcards as only the partial filename is known using the test Command, and when it exists then output just the number of lines in the file... do not include the filename. Then this output, is it captured by the CommandOutput or the ReturnValue as I want... (2 Replies)
Discussion started by: dsinco
2 Replies

3. Shell Programming and Scripting

Please help on UNIX grep command for numbers

I have a list of files like below, Do we have grep command to find files? If i grep 03874 it should display the file 3874, Grep command should ignore 0 at the beginning. There could be more many leading 0's in filename. $ ls -ltr total 5 -rw-r--r-- 1 mqm mqm 15 Feb 19 17:07 4769... (3 Replies)
Discussion started by: prince1987
3 Replies

4. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 Replies

5. Shell Programming and Scripting

Ksh: Test UNIX command without $? everytime

Hello all, working on Solaris 10 in ksh. Basicly, in my function, i'm trying to test that all my unix cmd's are true (exit status 0) else you flag the rcControlRule to 1 without going into spagetti mode code testing every $? in a if statement. The mdb is probably a little tricky cause it... (3 Replies)
Discussion started by: maverick72
3 Replies

6. Shell Programming and Scripting

Test command in UNIX

Hi Team, -rwxr-xr-x 1 kmani00 system 9 Nov 08 03:29 tempfile.txt -rwxrwxrwx 1 kmani00 devgrp 0 Nov 08 03:32 testfile.txt by exec the following command, i did not get any output. > test -s tempfile.txt > a=`test -s tempfile.txt` > echo $a > by exec the... (4 Replies)
Discussion started by: kmanivan82
4 Replies

7. Shell Programming and Scripting

grep functions, how to test if succeeded

Hello ...again. I am stuck on this part, I have a loop with processes an operations file. and calls different functions depending on what is in loop, which processes a database file... #so far my add function works as intended add() { ...blah blah; } # delete is kinda working... (13 Replies)
Discussion started by: gcampton
13 Replies

8. Shell Programming and Scripting

Using grep inside a test

Hi, I want to use grep inside a test statement, but I am getting an error message. Two variables testvarNum=5 testvarNonNum=x echo $testvarNum | grep * The result of this is as follows: 5 However, when I try the following (i.e. to test if the variable is numeric or non-numeric):... (3 Replies)
Discussion started by: dkieran
3 Replies

9. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

unix grep command

I need to seach all strings that matches "if ; then" in all files If i put grep "if ; then" *.* it is not giving any result (1 Reply)
Discussion started by: pmsuper
1 Replies
Login or Register to Ask a Question