[csh] checking for specific character ranges in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [csh] checking for specific character ranges in a variable
# 1  
Old 05-11-2008
[csh] checking for specific character ranges in a variable

I want to check if a zip code is valid, using a variable that stores the zipcode. I am not sure how I would do this in a script. I know that simply checking for the numerical range of the number will not work, because '1' would be '00001' in zip code format. I know when I am in shell, I can use wildcards and ranges to find certain files. How would I do something similar in C shell scripting? This following code is my attempt of checking for correct zip code format.

Code:
if ( $zipcode == "[0-9][0-9][0-9][0-9][0-9]" ) then
    echo Zip code is in correct format

# 2  
Old 05-11-2008
Code:
set zipcode=01234
test $zipcode="[0-9][0-9][0-9][0-9][0-9]" && echo Zip code is in correct format || echo NOK

# 3  
Old 05-11-2008
Quote:
Originally Posted by danmero
Code:
set zipcode=01234
test $zipcode="[0-9][0-9][0-9][0-9][0-9]" && echo Zip code is in correct format || echo NOK

Thanks. So it's not possible to check using the if statement?
# 4  
Old 05-11-2008
Yes it is but that's your homework Smilie
My example show you a basic AND/OR concept.
# 5  
Old 05-11-2008
Quote:
Originally Posted by danmero
Yes it is but that's your homework Smilie
My example show you a basic AND/OR concept.
How come my code in the OP does not work? Is there a different syntax I need for the condition of my if statement?
# 6  
Old 05-11-2008
Hi.
Quote:
Originally Posted by userix
How come my code in the OP does not work? Is there a different syntax I need for the condition of my if statement?
My specific suggestion is to look in man csh for "==".

My general suggestion is to avoid csh for scripting if at all possible. I often use tcsh for interactive work, but I use the Bourne shell family for scripting. It makes life far easier for me. See Csh Programming Considered Harmful for details.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

3. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

4. Shell Programming and Scripting

Generate Regex numeric range with specific sub-ranges

hi all, Say i have a range like 0 - 1000 and i need to split into diffrent files the lines which are within a specific fixed sub-range. I can achieve this manually but is not scalable if the range increase. E.g cat file1.txt Response time 2 ms Response time 15 ms Response time 101... (12 Replies)
Discussion started by: varu0612
12 Replies

5. Shell Programming and Scripting

Checking variable with specific string and stripping number from it.

I am parsing a file and I get differnt results everytime. Sometimes I get 12s sometimes I get 54m and sometime 3h.. v1=12s or v1=54m or v1=3h 12s - 12 seconds 54m - 54 minutes 3h - 3 hour I have to write a script in such a way that it whenever v1 is in minutes, I should strip "m"... (14 Replies)
Discussion started by: jayeshpatel
14 Replies

6. Shell Programming and Scripting

Checking for ranges based on 2 columns

Hi, I have a file with 6 columns. I want to check if column 1 and 2 fall between column 5 and 6 I want to call them as "TRUE_genes" if not then call them as "FALSE_genes". I can do it for checking one column but how to mention about two columns. file1 110371373... (1 Reply)
Discussion started by: Diya123
1 Replies

7. Shell Programming and Scripting

extracting columns falling within specific ranges for multiple files

Hi, I need to create weekly files from daily records stored in individual monthly filenames from 1999-2010. my sample file structure is like the ones below: daily record stored per month: 199901.xyz, 199902.xyz, 199903.xyz, 199904.xyz ...199912.xyz records inside 199901.xyz (original data... (4 Replies)
Discussion started by: ida1215
4 Replies

8. Shell Programming and Scripting

getting files between specific date ranges in solaris

hi ! how can i get files in a directory between certain date ranges ? say all files created/modified between Jan24 - Jan31 thanks (10 Replies)
Discussion started by: aliyesami
10 Replies

9. UNIX for Dummies Questions & Answers

checking wether an inputed character is already in a variable

Hi, i have a variable which holds a variety of letters. eg, var=qwertyuiop what i want to do is determine wether an inputed letter is already stored inside the variable, so i can say to enter a new one. i have been playing around using tr and grep but nothing seems to work at all. ... (2 Replies)
Discussion started by: castillo
2 Replies

10. Shell Programming and Scripting

How to read the value from a specific line and column in to a csh variable

Hi All, Although its a basic question the last 2 hours of googling and trying didnt help me to achieve what i want. Maybe some one can help me I have a text file text1.txt: blablablabla A B C D E F and i would like to read to read what is on position E (line 3 column 2) in a... (2 Replies)
Discussion started by: Radamez
2 Replies
Login or Register to Ask a Question