Regex pattern for multiple digits


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Regex pattern for multiple digits
# 1  
Old 12-29-2010
Regex pattern for multiple digits

Hello,

I need to construct a pattern to match the below string (especially the timestamp at the beginning)

20101222100436_temp.dat

The below pattern works [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_temp.dat

However I am trying find if there are any other better representations.
I tried [\d]{14}, but it did not work.

I am on RedHat Linux 2.6.

Thanks.
# 2  
Old 12-29-2010
It will depend on what program you are using the regex on, grep, sed, perl, awk, etc.

Use just \d, as [\d] will match "d" only.
# 3  
Old 12-29-2010
I am trying this on a ls program.

ls -l \d{14}_temp.dat

The above command does not give the expected result. I'm trying to fish the file 20101222100436_temp.dat.
# 4  
Old 12-29-2010
Most commonly used shells do not support \d for file-globbing. If you are using bash, the command line you entered would be interpreted as:
Code:
ls -l d{14}_temp.dat

You are going to have to stick with, as you put it, the "pattern that works".
This User Gave Thanks to m.d.ludwig For This Post:
# 5  
Old 12-29-2010
File globbing is not regex. You have ? not ., * not .*, [abc] in both, but not [^abc] or * or {##} of regex!

You can model date ranges more accurately, e.g., for 1900-2099:
Code:
 
  [21][90][0-9][0-9][01][0-9][0-3][0-9][0-2][0-9][0-5][0-9[0-5][0-9]_tmp.dat

You could write your dates into the file name with a little punctuation, for clarity and mistake prevention, like:
Code:
 
  2010-12-29_14:56:07_temp.dat

# 6  
Old 12-30-2010
If less accuracy is allowed you could use extended globbing in ksh93/bash
Code:
ls -l +([0-9])_temp.dat

( in bash use shopt -s extglob if extended globbing is not in by default )

--
In a recent ksh93 (version l or later) you can even do exact matching like this:
Code:
ls -l {14}([0-9])_temp.dat

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. Shell Programming and Scripting

Regex find first 5-7 occurrences of a set of digits within a string

Using these strings as an example: <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title=""> <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451486&amp;playTogether=True',960,540,94343);return false;" title=""> <a... (12 Replies)
Discussion started by: metallica1973
12 Replies

5. Shell Programming and Scripting

Regex - Return numbers of exactly 8 digits

Hi All I am new to this forum and also regex. I am using bash scripting and have a file like this "0012","efgh","12345678","adfdf", "36598745" "87654321","hijk","lmno" I want the ouput to be 12345678 36598745 87654321 Criteria like this - number - 8 carachters long Please let... (21 Replies)
Discussion started by: buttseire
21 Replies

6. Shell Programming and Scripting

regex to match digits not in dates

hi all, im having problems. I need to change all number 10 in a text file to word form, or in short from 10->ten. the thing is number 10 including in dates such as 10/22/1997 or 03-10-2011 should not be changed. im having some trouble because the file contains numbers like "price range from... (11 Replies)
Discussion started by: perlishell
11 Replies

7. UNIX for Dummies Questions & Answers

how to use grep: finding a string with double quotes and multiple digits

I have a file with a lot of lines (a lot!) that contain 10 digits between double quotes. ie "1726937489". The digits are random throughout, but always contain ten digits. I can not for the life of me, (via scouring the internet and grep how-to manuals) figure out how to find this when I search.... (3 Replies)
Discussion started by: titusbass
3 Replies

8. Shell Programming and Scripting

Isolate and Extract a Pattern Substring (Digits Only)

Hi guys, I have a text file report generated from egrepping multiple files. The text files themselves are obtianed after many succesive refinements, so they contain already the desired number, but this is surrounded by unwanted characters, newlines, spaces, it is not always at the start of the... (6 Replies)
Discussion started by: netfreighter
6 Replies

9. Shell Programming and Scripting

Split file into multiple files depending upon first 4 digits

Hi All, I have a file like below: 1016D"ddd","343","1299" 1016D"ddd","3564","1299" 1016D"ddd","3297","1393" 1016D"ddd","32989","1527" 1016D"ddd","346498","1652" 2312D"ddd","3269","1652" 2312D"ddd","328","1652" 2312D"ddd","2224","2100" 3444D"ddd","252","2100" 3444D"ddd","2619","2100"... (4 Replies)
Discussion started by: deepakgang
4 Replies

10. Shell Programming and Scripting

How to pattern match on digits and then increment?

I have a log file that ends in a ".xxx" where xxx are digits but I don't necessarily know what digits they are. The log file rotates automatically and is auto-incrementing - starting at .001. So the example would be: file-name.005 If the file ends in .005 and the log rotates, it logically... (2 Replies)
Discussion started by: sdutto01
2 Replies
Login or Register to Ask a Question