Convert .../($var1|$var2)/... regex to string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert .../($var1|$var2)/... regex to string
# 1  
Old 08-02-2012
Java Convert .../($var1|$var2)/... regex to string

Hello,

In one part of my shell program I need to translate many of lines in the following pattern :
Code:
/(folder1|...|folderN)/(sub1|...|subN)/.../(file1|...|fileN)

into strings :
Code:
/folder1/sub1/.../file1
/folder1/sub1/.../...
/folder1/sub1/.../fileN
...
/folderN/subN/.../fileN

the folderX/fileX is just a variable and it could be any name.

Please help me obtaining the shell script.

sample input :
Code:
/(ar|az|dua|en|fa|fr)/(index|index2).php
/(ar|az|dua|en|fa|fr)/administrator/(index|index2).php
/(ar|fa)/modules/(mod_tabmods|mod_tabs|mod_base|mod_secure)/scripts/(importer|uploader).php

desired output :
Code:
/ar/index.php
/ar/index2.php
/az/index.php
/az/index2.php
/dua/index.php
/dua/index2.php
/en/index.php
/en/index2.php
/fa/index.php
/fa/index2.php
/fr/index.php
/fr/index2.php
/ar/administrator/index.php
/ar/administrator/index2.php
/az/administrator/index.php
/az/administrator/index2.php
/dua/administrator/index.php
/dua/administrator/index2.php
/en/administrator/index.php
/en/administrator/index2.php
/fa/administrator/index.php
/fa/administrator/index2.php
/fr/administrator/index.php
/fr/administrator/index2.php
/ar/modules/mod_tabmods/scripts/importer.php
/ar/modules/mod_tabmods/scripts/uploader.php
/ar/modules/mod_tabs/scripts/importer.php
/ar/modules/mod_tabs/scripts/uploader.php
/ar/modules/mod_base/scripts/importer.php
/ar/modules/mod_base/scripts/uploader.php
/ar/modules/mod_secure/scripts/importer.php
/ar/modules/mod_secure/scripts/uploader.php
/fa/modules/mod_tabmods/scripts/importer.php
/fa/modules/mod_tabmods/scripts/uploader.php
/fa/modules/mod_tabs/scripts/importer.php
/fa/modules/mod_tabs/scripts/uploader.php
/fa/modules/mod_base/scripts/importer.php
/fa/modules/mod_base/scripts/uploader.php
/fa/modules/mod_secure/scripts/importer.php
/fa/modules/mod_secure/scripts/uploader.php


Last edited by Ghadamyari; 08-02-2012 at 09:16 AM..
# 2  
Old 08-02-2012
Post withdrawn...didn't see your sample input and expected output Smilie
# 3  
Old 08-02-2012
"Your shell program"... Which shell?

For instance, in bash you can use brace expansion:

Code:
echo /{ar,az,dua,en,fa,fr}/{index,index2}.php
/ar/index.php /ar/index2.php /az/index.php /az/index2.php /dua/index.php /dua/index2.php /en/index.php /en/index2.php /fa/index.php /fa/index2.php /fr/index.php /fr/index2.php

If you have stored the string in a variable, say $input, then:

Code:
input="/{ar,az,dua,en,fa,fr}/{index,index2}.php"
eval echo "$input"
/ar/index.php /ar/index2.php /az/index.php /az/index2.php /dua/index.php /dua/index2.php /en/index.php /en/index2.php /fa/index.php /fa/index2.php /fr/index.php /fr/index2.php

So, first change the format from "/(ar|az|dua|en|fa|fr)/(index|index2).php" to "/{ar,az,dua,en,fa,fr}/{index,index2}.php". After you've checked that neither "{", "}", nor "," are present in your paths, do it this way:

Code:
input_forBE="$(sed -e 's/(/{/g' -e 's/)/}g' -e 's/|/,/g'  <<<$input)"

Then use bash brace expansion, as shown above. For instance:
Code:
eval ls "$input_forBE"
ls: impossibile accedere a /ar/index.php: File o directory non esistente
ls: impossibile accedere a /ar/index2.php: File o directory non esistente
ls: impossibile accedere a /az/index.php: File o directory non esistente
ls: impossibile accedere a /az/index2.php: File o directory non esistente
ls: impossibile accedere a /dua/index.php: File o directory non esistente
ls: impossibile accedere a /dua/index2.php: File o directory non esistente
ls: impossibile accedere a /en/index.php: File o directory non esistente
ls: impossibile accedere a /en/index2.php: File o directory non esistente
ls: impossibile accedere a /fa/index.php: File o directory non esistente
ls: impossibile accedere a /fa/index2.php: File o directory non esistente
ls: impossibile accedere a /fr/index.php: File o directory non esistente
ls: impossibile accedere a /fr/index2.php: File o directory non esistente

--
Bye

Last edited by Lem; 08-02-2012 at 10:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert posix regex to pcre (perl)

In order to clean file from html tags i used the following sed 's/<*>//g' filename Right now i need to do the same from php script so i have to use pcre. How to convert? (1 Reply)
Discussion started by: urello
1 Replies

2. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

3. Shell Programming and Scripting

concatenate varaibles var1, var2 together to var1 again

HI i like to concatenate the two variables var1=$line (ie 22885068900000652 B86860003OLFXXX592123320081227) var2=$amount (ie 123456) i want to club together both the above varaible var1 & var2 and assign back to var1 right now i am doing like this but it is not working. var1=`echo... (1 Reply)
Discussion started by: kshuser
1 Replies

4. Shell Programming and Scripting

BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ? #!/bin/perl -w my $VAR = "some value"; if ( $VAR =~... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

using regex to get part of a string ?

Hi there, i wonder, is it possible to use regular expressions to partially select a string? I have a bunch of server names which look like this server1z-test server2z2 server45z-primary server13z3 I want to extract up to and including the 'z' in the server name, so for example ... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

6. UNIX for Dummies Questions & Answers

What is this? var2=${var1#??????????}

Googling the answer to this question just doesn't work when Google won't search your symbols for you. Can someone tell me what this command will assign to var2, and where I can find more information about using braces in this way? var2=${var1#??????????} Thanks so much. (2 Replies)
Discussion started by: blondie53403
2 Replies

7. UNIX for Dummies Questions & Answers

regex on first string in a variable.

Hi all, this is driving me nuts. I need to evaluate if a variable in a shell script has a heading s or m character e.g. s92342394 or m9233489 if so then I need to get rid of them. I'm quite familiar with PERL and could do it there in 3 mins but I have not found a decent way to do this in a shell.... (1 Reply)
Discussion started by: Endo
1 Replies

8. Shell Programming and Scripting

(probably stupidly simple) if [ $var1 = $var2 ] problem...

OK, while I'm not new to the Mac or some of the the inner workings of Mac OS X I am quite new to scripting. And while I have "Learning the Bash Shell" by those lovely people at O'Reilly (Cameron Newham et. al.) I'm missing something, I just know I am. Here's the problem: At the beginning of... (2 Replies)
Discussion started by: woodgie
2 Replies

9. Shell Programming and Scripting

Echo var1,var2,var3,var4,var5,var6 in loop help

Hello, I have created numerous variables called $mask1 $mask2... $maskN. I wish to echo these variables, below is the code for the two functions, the first creates the variables and the second echos them. It is the second function which does not work. The line (i believe to be wrong) is in... (1 Reply)
Discussion started by: rorey_breaker
1 Replies

10. UNIX for Dummies Questions & Answers

*fixed if (( var1 && var2 )) ??

*EDIT -- **FIXEd I must've done something wrong, because it works now.. I punched in "if" in the forum search but nothing came up, so I'm posting it here. Quick question: Can someone correct my syntax. #!/bin/ksh if (( var >= 1 && var <= 5 )); then .................................. (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question

Featured Tech Videos