Using a reverse regex to create a number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using a reverse regex to create a number
# 1  
Old 05-11-2012
Using a reverse regex to create a number

Hi all,

I'm having an issue about a code i should write...
I have a file... with the following numbers in regex format:

$ cat file_regex.txt
Code:
555005080[78]07[0-9]*
55500218200[35][0-9]* 
182936[01][0-9]* 
182929[78][0-9]* 
4179[012356789][0-9]* 
381[23][89][0-9]* 
550069341[0-9]*

So this is a file cointaing some regex... so for each regex i need to create a number which is compatible to this regex... for example:

This:
Code:
555005080[78]07[0-9]*

Should transform into:
Code:
5550050807070



So what i need to do... is whenever is this file when there's some number into [], i need to remove this tags and get the first occurrence inside the brackets... so my outfile file will look like this:

$ cat output_file.txt
Code:
5550050807070
5550021820030 
18293600 
18292970 
417900 
381280 
5500693410

Any good ideas of how doing it?
I thought doing some thing with cut... but the way i thought... code will be very ugly, disgusting and not performative lol

Thanks

Last edited by Scrutinizer; 05-11-2012 at 01:14 PM.. Reason: code tags
# 2  
Old 05-11-2012
Code:
perl -pe 's/\[(\d)[^\]]*\]\*?/\1/g' file_regex.txt > output_file.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 05-11-2012
Code:
sed 's/\[\([0-9]\)[^]]*\][*?]*/\1/g' file_regex.txt > output_file.txt

This User Gave Thanks to neutronscott For This Post:
# 4  
Old 05-11-2012
Wow! Thanks... both solutions just worked fine!

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract whole number or decimal in regex

In the perl below I am trying to extract and print specic values from patterns using multiple regex. One of the patterns AF= may be a whole number or a decimal but I can not seem to capture both. I think it is the regex .*AF=(\d+\.\d+); as it is expecting a #.#### and it may only be a #. I tried... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

3. Shell Programming and Scripting

Reverse specific field number

Hello everybody :) I have specific problem when i use rev | uniq -f 3 -c | revORIGINAL Output- (without rev | uniq -f 3 -c | rev) tibenska13 Oct 30 00:26firsth revOUTPUT- 62:00 03 tcO 31aksnebitafter uniq -f 3 -cOUTPUT- 19 62:00 03 tcO 31aksnebitafter next revOUTPUT- tibenska13 Oct 30 00:26... (7 Replies)
Discussion started by: netsys
7 Replies

4. Solaris

Help to create a regex for this policy

Help with creating regex in tripwire : the rule is " The idea of it looks to ensure that just ‘share' isn't used in dfstab, must be /usr/sbin/share" Perform the following to determine if the system is configured as recommended: # grep -v '^#' /etc/dfs/dfstab | grep 'share' | grep -v... (1 Reply)
Discussion started by: bathija12
1 Replies

5. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

6. Shell Programming and Scripting

Better and efficient way to reverse search a file for first matched line number.

How to reverse search for a matched string in a file. Get line# of the first matched line. I am getting '2' into 'lineNum' variable. But it feels like I am using too many commands. Is there a better more efficiant way to do this on Unix? abc.log aaaaaaaaaaaaa bbbbbbbbbbbbb... (11 Replies)
Discussion started by: kchinnam
11 Replies

7. Shell Programming and Scripting

Reverse regex logic

Hi, I'm trying to reverse regex logic to use it in grep command. I would like to grep a string within a file that contains regex. For example file example.txt contains line: match* And I would like to find it using grep match123 example.txt Is it possible? Thank you very much for all... (4 Replies)
Discussion started by: Ajgor99
4 Replies

8. Shell Programming and Scripting

help for fast way of finding line number for a regex

Hello, I am trying to find out the line numbers where regex match and put them into a file with below command: awk '/'$pat'/ {print NR}' $fileName >> temp.txt where $pat is the regex but this command is taking a lot of time to execute with bigger files for size more than 5000000... (8 Replies)
Discussion started by: JoeColeEPL9
8 Replies

9. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

10. UNIX for Dummies Questions & Answers

using sed and regex to reverse order???

so i have been trying to learn how to manipulate text on my own and have gotten stumped... let's say i have a text file that says (highly simplified): people ordinary How would swap the order of the words.. I know i need to use sed and some kind of back reference but cannot make it... (2 Replies)
Discussion started by: urtherhoda
2 Replies
Login or Register to Ask a Question