Check whether there is only numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check whether there is only numbers
# 1  
Old 12-30-2009
Check whether there is only numbers

Hi everyone,

I must make a script which will be executed as follows :

./script.sh Directory/n[0-9].car where there can be as many numbers as we may want.

I'd like to : - ignore the Directory ; Meaning I just need n[0-9].car
- Check the name of the file : has it got some Alphabetic Characters after the n? in which case it's not a proper file name.
- *Extract* the number in the name, to be able to compare it to something else after.

Thanks in advance,

~ Lyth_o ~
# 2  
Old 12-30-2009
At least an idea to work with:

Code:
FILE=$( basename $1 )
CORE=$( echo $FILE | sed '/s/^n//' | sed 's/$\.car//' )
TEST=$( echo $CORE | egrep -o '[[:alpha:]]' )
if [ -n "$TEST" ]
then
  echo "File name not valid!"
fi

# 3  
Old 12-30-2009
Ok thank you.
I'll try to understand sed's use. (I have never understood what it does or how to use it...)
^^
# 4  
Old 12-30-2009
Presuming you do not literally mean ./script.sh Directory/n[0-9].car which is a globbing expression which may expand to multiple files as input, but that you mean a single file that consists of an n followed by multiple digits (n>=1) followed by the extension .car:

If so, you could try this alternative which uses no external programs:
Code:
no_n="${1##*/n}"
nr="${no_n%.car}"
case $nr in
  *[^0-9]*) echo "File name ${1##*/} invalid.."
esac

# 5  
Old 12-30-2009
Here is another way - remove digits from the basename of the file spec and you get "n.car" for good files.

Code:
if [[  $(basename $filename | tr -d '[:digit:]') = "n.car" ]] ; then
   echo "$filename is ok"
else
   echo "$filename is not ok"
fi

# 6  
Old 12-30-2009
Bash/ksh:
Code:
if [[ ! ${1//[0-9]} =~ .*/?n.car ]]; then
  echo "File name ${1##*/} invalid.."
fi

# 7  
Old 01-02-2010
Thank you everyone.

I took Scrutinizer's first option.

Does your second option do the same?

Do you know how to get the last character of a line?

I have the code :

echo `$1 $2`

And I would like to compare the last character of the result (which is a number) with a certain number...

Edit : I have found something working...

But, does someone know how to echo something when executing `$1 $2` ($1 is ./program and $2 an argument of it) provokes an error?

Last edited by Lyth_o; 01-02-2010 at 06:52 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string contains substring surrounded by numbers

Hi, I have a process that generates strings. I would like to check each string and search for substring which contains the letter 'E' surrounded by numbers (both sides of the letter 'E'). few examples: AA4E7012A2 - contains E surrounded by numbers FE18274012 - does not contain E... (3 Replies)
Discussion started by: yanive
3 Replies

2. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

4. Shell Programming and Scripting

Check if a string starts with certain values and ends with numbers

This is very basic. Yet Iam struggling to get the right pattern for my check. Apologize in advance to ask a very lame question. I have to validate if a value of the variable starts with "efgh" and followed by 6 numbers. Var1="efgh234567" The condition Iam trying to achieve is similar to... (6 Replies)
Discussion started by: deepakwins
6 Replies

5. Shell Programming and Scripting

Request to check:remove entries with duplicate numbers in first row

Hi I have a file 1 xyz 456 1 xyz 456 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 4 ghf 658 I want the output 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 (3 Replies)
Discussion started by: manigrover
3 Replies

6. Shell Programming and Scripting

check for numbers

Hi, I have a requirement to accet only numbers. Piece of code beolow. waitTime() { echo "Enter amount of time to wait (in sec)." read wait_time sleep $wait_time } the above function should accept only numbers otherwise, it has t throw an error. Please help. (2 Replies)
Discussion started by: Satyak
2 Replies

7. Shell Programming and Scripting

Shell script to check numbers!

Hello All, I have 3 types of files. The names of which starts with P,I,M like P********* D********* M********* now I need to do some operations witht hese files.. so if file name starts with P or p then do the operation for P file... fi else (20 Replies)
Discussion started by: smarty86
20 Replies

8. Shell Programming and Scripting

Regular expressions to check numbers with currency

Hi All I am struggling for the last one week on how to write a regular expression to search a number with currency (such as $ 1, 245, 000.00, Rs. 1, 00, 000.00 etc) but in vain . Please help me. Thanks in advance. -----Post Update----- Hi All I am struggling for the last one... (7 Replies)
Discussion started by: my_Perl
7 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. Shell Programming and Scripting

Shell script to check the unique numbers in huge data

Friends, I have to write a shell script,the description is---- i Have to check the uniqueness of the numbers in a file. A file is containing 200thousand tickets and a ticket have 15 numbers in asecending order.And there is a strip that is having 6 tickets that means 90 numbers.I... (7 Replies)
Discussion started by: namishtiwari
7 Replies
Login or Register to Ask a Question