Regex - Return numbers of exactly 8 digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex - Return numbers of exactly 8 digits
# 1  
Old 08-22-2012
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
Code:
"0012","efgh","12345678","adfdf", "36598745"
"87654321","hijk","lmno"

I want the ouput to be
Code:
12345678
36598745
87654321

Criteria like this
- number
- 8 carachters long

Please let me know what regexc a satisy my reqs

Thanks in advance!

B


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-22-2012 at 03:31 AM.. Reason: code tags
# 2  
Old 08-22-2012
Welcome to this forum...Smilie

try this...

Code:
awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } } ' file2

# 3  
Old 08-22-2012
Hi Pamu

Thanks a mil for the quick response!

I have tried in both ways

Code:
cat file1.txt | awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } } '

and

Code:
awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } } ' file1.txt

(file1.txt being the text example in the 1st post)

...but i get

Code:
awk: can't open { for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } }

please advise (and thanks again!)


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-22-2012 at 03:32 AM.. Reason: code tags
# 4  
Old 08-22-2012
Quote:
Originally Posted by pamu
Welcome to this forum...Smilie

try this...

Code:
awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } } ' file2

Hi pamu,
Your script will print any 8 character quoted field as long as it contains at least one digit. In addition to strings like "12345678" it will also match a string like "1bcdefgh" and "abc4efgh". If someone just wants 8 character quoted strings with each character being a digit, this should work:
Code:
awk -F '"' '{ for(i=1;i<=NF;i++) if($i ~ "^([0-9]){8}$") print $i }' input

# 5  
Old 08-22-2012
Quote:
Originally Posted by pamu
Welcome to this forum...Smilie

try this...

Code:
awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8))   { print $i } } } ' file2

The output of that code is not limited to 8 digit numbers. It will print out any field of 8 characters with at least one digit.

You can set RS and then just use a single regex:
Code:
awk '/^[0-9]{8}$/' RS=\"

Or, without using awk:
Code:
tr \" \\n | grep '^[0-9]\{8\}$'

Regards,
Alister
# 6  
Old 08-22-2012
Quote:
Originally Posted by buttseire
Hi Pamu

Thanks a mil for the quick response!

I have tried in both ways

cat file1.txt | awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8)) { print $i } } } '

and

awk -F '"' '{ for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8)) { print $i } } } ' file1.txt

(file1.txt being the text example in the 1st post)

...but i get

awk: can't open { for (i=1;i<=NF;i++) { if ( ($i ~ /[0-9]/) && (length($i) == 8)) { print $i } } }

please advise (and thanks again!)
I wouldn't expect to see that error message unless you had a -f option after the -F option and its option-argument. Are you sure you entered the command exactly as shown above? What shell are you using? What is the output from the command uname -a on the system you're using?
# 7  
Old 08-22-2012
A perl alternative:
Code:
perl -lne 'while (m/(\d{8})/g){print $1}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

RegEx find numbers above 25000

Hello, I am using the Sublime Plugin LogHighlight. I can use RegEx there to highlight some lines in sublime. Now I need to find every line, that has a number of above 25000. the lines look like this: smart_sdl.result: 8947 smart_sdm.result: 8947 smart_sdn.result: 25000 Currently I am... (3 Replies)
Discussion started by: blend_in
3 Replies

2. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 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. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. Shell Programming and Scripting

Sed only digits not in numbers

Hi, I have a text file with an array of numbers such as : 123 1 456 45 9817 1 45 I would like to replace the digit "1" in a text file with "A". So it looks like this: 123 A 456 45 9817 A 45 If I use sed 's/1/A/g', I get A23 A 456 45 98A7 A 45 I... (3 Replies)
Discussion started by: jejeking
3 Replies

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

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

9. Shell Programming and Scripting

3 numbers and return the maximum

Hello, I am doing bash and for that doing excersices. The folowing one asks for 3 numbers and is suppose to return the maximum. An error should be returned if on of the numbers is missing. I managed to solve it. Can you give me other ways or advices. #!/bin/bash #Date: 2010.10.19 #Ecrire un... (5 Replies)
Discussion started by: flash80
5 Replies

10. Shell Programming and Scripting

AWK regex to find only numbers

Hi guys I need to find both negative and positive numbers from the following text file. And i also dont need 0. 0 8 -7 -2268 007 -07 -00 -0a0 0a0 -07a0 7a00 0a0 Can someone please give a regex to filter out the values in red. I tried a few things in awk but it didnt work... (9 Replies)
Discussion started by: sridanu
9 Replies
Login or Register to Ask a Question