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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex find first 5-7 occurrences of a set of digits within a string
# 1  
Old 03-07-2013
Regex find first 5-7 occurrences of a set of digits within a string

Using these strings as an example:
Code:
<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 onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451489&amp;playTogether=True',960,540,94343);return false;" title="">
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=45148&amp;playTogether=True',960,540,94343);return false;" title="">

[code]
Using a regex espression, how can I extract just the first 5-7 digits of a string(anywhere in the string)and end there? So in this case I only want to print out only the "first" set of 5-7 digits of a string which would give me an output of:
Code:
145148
1451486
1451489
45148

and "not" several sets on the same string
Code:
145148 943437
45148 94343

I tried:
Code:
\d{5,7}

and it grabs every occurrence on the same line??

Last edited by metallica1973; 03-07-2013 at 11:53 AM..
# 2  
Old 03-07-2013
making some assumptions:
Code:
awk -F'[=&]' '{ print $4}' myFile

# 3  
Old 03-07-2013
I will go for awk -F = '{print $4}' filename|awk -F '&' '{print $1}' ,as no assumptions needed.

I like the approach of vgersh99 though. By the way, vgersh99, can u pls explain how it processes for the next match ?? a bit confused why it prints $1 instead of $4 in next pattern match...
# 4  
Old 03-07-2013
I appoligize, I meant using a regular expression so,using a regex expression, how can I extract just the first 5-7 digits of a string(anywhere in the string)and end there? So in this case I only want to print out only the "first" set of 5-7 digits of a string which would give me an output of:
Code:
145148
1451486
1451489
45148


Last edited by metallica1973; 03-07-2013 at 12:01 PM..
# 5  
Old 03-07-2013
Quote:
Originally Posted by sangfroid
I will go for awk -F = '{print $4}' filename|awk -F '&' '{print $1}' ,as no assumptions needed.

I like the approach of vgersh99 though. By the way, vgersh99, can u pls explain how it processes for the next match ?? a bit confused why it prints $1 instead of $4 in next pattern match...
you're making the same assumption as I'm. The diff is that I'm field separators either '=' or '&' - and you're using 2 awk-s piped together.
# 6  
Old 03-07-2013
Many thanks for the reply,

I modified my original post to reflect what I need but a quick summary:

Sample Strings
Code:
<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 onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451489&amp;playTogether=True',960,540,94343);return false;" title="">
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=45148&amp;playTogether=True',960,540,94343);return false;" title="">

Code:
Want I want to get out of my regular expression:

Code:
145148
1451486
1451489
45148

and "not" several sets on the same string
Code:
145148 943437
45148 94343

I tried:
Code:
\d{5,7}

and it grabs every occurrence on the same line??
# 7  
Old 03-07-2013
using gawk:
Code:
gawk --posix '{if (match($0,"[0-9]{5,7}")) print substr($0, RSTART, RLENGTH)}' myFile

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

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

3. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

4. 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

5. UNIX for Advanced & Expert Users

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 _temp.dat However I am trying find if there are any other better representations. I tried {14}, but it did not work. I am on... (5 Replies)
Discussion started by: krishmaths
5 Replies

6. Shell Programming and Scripting

string pattern ,files ,occurrences

Hi , I want to create a shell script which will find the complexity of a sql script.Suppose a.sql holds the following two query -- select * from table1 a ,table2 b where a.column1=b.column1; select * from table1 a ,(select * from table1 c ,table2 d where c.column1=d.column1) e where... (3 Replies)
Discussion started by: anupamhalder
3 Replies

7. Shell Programming and Scripting

how to find a particular string from a set of string output

Hi , I have a line by line output as follows,for example output of ls sample1 sample2 sample i need to check if this output contains the exact string sample.If i use grep , it will find out all strings that contain sample as a part of their word.I dont want to do a pattern matching... (11 Replies)
Discussion started by: padmisri
11 Replies

8. Shell Programming and Scripting

find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format. ua07app201 (20 Replies)
Discussion started by: honglus
20 Replies

9. UNIX for Dummies Questions & Answers

regex - display all occurrences of match

i don't want to display the whole line but i want to display all the string(s) that match the Regex, even if their are more then one match per line in my file. data: mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql $AI_SQL/wkly_inqry_trtry.sql $AI_SQL/wkly_nb_trtry.sql \... (18 Replies)
Discussion started by: danmauer
18 Replies

10. Shell Programming and Scripting

validation: set field size to 6 digits

I'm curious, how would I set validation on a field size to make sure it only ever accepts a 6 digit number? I currently have validation for it to be an integer in place but can't find anywhere that says how to make it only a length of 6 digits. any tips on the function I should be focussing... (1 Reply)
Discussion started by: nortypig
1 Replies
Login or Register to Ask a Question