Using regex in sed to validate the length of an entry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using regex in sed to validate the length of an entry
# 1  
Old 04-28-2010
Using regex in sed to validate the length of an entry

I'm having trouble using sed to validate the length of an entry. I want to have a user enter a phone number of either length 7, 10 or 11. Only numbers are allowed. Does anyone know how to do this? Here's the code I have so far. It only validates that numbers are entered but not the length.


Code:
#make sure Phone Number only contains numbers
      while [[ ! -z $(echo $phone | sed 's/[0-9]//g') ]]; do
         echo ""
         printf "Please Enter The Contacts Phone Number (only numbers are allowed): "
         read phone
      done

# 2  
Old 04-28-2010
try using awk..
code will be something like this..
this will check if the phone no has seven no and print the length
Code:
echo "$phone"|awk '/[0-9]{7}/{print length($0)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to validate parameter for sleep

I'm trying to use a regular expression to validate a value for sleep entered by the user. The test should fail all negative values and 0 but let pass all combinations of + . and digits that would amount to a valid parameter for sleep. Examples for valid: 1, 1.5, .5, 0.5, +1, +.5, +1.3 etc.... (9 Replies)
Discussion started by: Ralph
9 Replies

2. Shell Programming and Scripting

sed command to add a new column entry

My input file looks like this 12 3 5.122.281.413 172.31.15.220 3421 4133 2 2 1454 3421 4133 2 2 0 12 44036 214.215.52.146 90.123.245.211 2312 3911 4 4 521 2312 3911 4 4 1 14 504 6.254.324.219 192.61.27.120 4444 5611 7 5 1415 4444 5611 7 5 1 ... (2 Replies)
Discussion started by: sampitosh
2 Replies

3. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

4. Shell Programming and Scripting

Filter (by max length) only lines not matching regex

I have a large file of many pairs of sequences and their headers, which always begin with '>' I'm looking for help on how to retain only sequences (and their headers) below a certain length. So if min length was 10, output would be I can filter by length, but I'm not sure how to exclude... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Replacing specific entry using sed

Hi guys, I'm new to bash programming, so please pardon me. I'm trying to replace an entry's text in Books.txt This code works perfectly: sed -i "s/$BookTitle/$NewBookTitle/g" Books.txt But problem is, if there are double entries, it will also replace that entry. For example: ... (12 Replies)
Discussion started by: todaealas
12 Replies

6. Shell Programming and Scripting

sed remove cron entry

Hello, I am not having much luck with removing a entry in cron. I would like to get rid of the following line in /var/spool/cron/root. 1 09 * * 1-5 /etc/init.d/snmpd stop > /dev/null 2>&1I have tried this with no luck. sed -i 's#1 09 * * 1-5 /etc/init.d/snmpd stop >... (3 Replies)
Discussion started by: jaysunn
3 Replies

7. Shell Programming and Scripting

parse xm entry with awk/sed

Hi folks, I have XML files with the following sections (section occurs once per file) in them: <AuthorList CompleteYN="Y"> <Author ValidYN="Y"> <LastName>Bernal</LastName> <ForeName>Federico</ForeName> ... (3 Replies)
Discussion started by: euval
3 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

10. UNIX for Dummies Questions & Answers

Muilti Entry Sed

My defaul shell is /bin/ksh I want to make multi entry sed changes to files mainly because is easier for me to check my arguments in that format, than all in just one line. example sed ' > s/this/for this/ > s/that/for that/ > s/I/me/' filex > filez Searching the net I read... (2 Replies)
Discussion started by: tony3101
2 Replies
Login or Register to Ask a Question