LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2012, 06:48 AM   #1
smartgig
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Rep: Reputation: Disabled
Post 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
Please help me obtaining the shell script.
 
Old 08-02-2012, 06:59 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Hi and welcome to LQ.

Can you post the actual example of your naming conventions?

Do the names of the directories/files differ only in the appended number?
 
Old 08-02-2012, 07:04 AM   #3
smartgig
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Here are some of them :
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
I want to apply some command on the files so the path of the files should be in the correct way !
 
Old 08-02-2012, 07:06 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Now you've confused me. That's not exactly what you posted in the original post.
Can you post the desired output of the sample above?
 
Old 08-02-2012, 07:15 AM   #5
smartgig
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you for your answer !
Where's the confusion?

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
 
Old 08-02-2012, 08:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Well, this is a challenging one!

A proper solution would likely involve parsing the lines and storing each part separately into arrays, which would be re-built into the desired output, or something like that. It would take some time to figure out.

I do have a quick hack solution, but I don't particularly recommend it.

Code:
text='/(ar|az|dua|en|fa|fr)/administrator/(index|index2).php'
text="$( echo "$text" | tr '()|' '{},' )"
eval printf '%s\\n' "$text"
This translates the line into a valid brace expansion. Unfortunately though the final printing involves using eval to expand it, and eval has worrisome security issues. I generally suggest avoiding it unless there's no other choice. It may be safe enough here though, as long as the input text is known and trustworthy.

It also won't work correctly if any part of the original line contains whitespace, or if there happen to be '()|' characters that aren't part of the expansion pattern.

(Incidentally, I tried to use parameter substitutions instead of tr, but there seems to be a bug in my bash that keeps the substitution to "}" from being handled correctly.)
 
Old 08-02-2012, 10:59 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by smartgig View Post
Thank you for your answer !
Where's the confusion?

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
My python is not great but it seems to work fine:
Code:
cat text.txt 
/(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
~/data/tmp % ./pyscri.py 
/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
pyscri.py
Code:
#!/usr/bin/env python
import re
import itertools

f_ext = ".php"
new_path = []
new_line = []
lines = [line.rstrip() for line in open('text.txt')]

for x in range(len(lines)):
    each_line = lines[x].split("/")
    for z in range(1, len(each_line)):
        level1 = (re.sub('\(|\)', '', each_line[z])).split("|")
        new_path.append(level1)
        if f_ext in level1[-1]:
            new_line.append(new_path)
            new_path = []

for paths in range(len(new_line)):
    for element in itertools.product(*new_line[paths]):
        final_line = '/' + '/'.join(element)
        if f_ext not in final_line:
            final_line = final_line + f_ext
        print(final_line)
HTH

Last edited by sycamorex; 08-02-2012 at 11:01 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
diff $Var1 $Var2 ? Xeratul Programming 8 01-13-2012 03:49 PM
[SOLVED] Trying to concatenate two variables and a slash ($var1/$var2). jbulgier Linux - General 2 12-13-2011 11:52 PM
[SOLVED] bash regex string extraction porphyry5 Programming 2 06-10-2011 01:34 PM
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 08:32 PM
bash: how to divide complex : FINAL=`[ ($var2 - $var1 ) / $var1 * 100 ]` frenchn00b Programming 9 11-06-2008 11:04 AM


All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration