Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed and rev shell command into Python script

There is a shell command, I am trying to convert the logic into python. But I don’t know what to do, I need some help with that.

shell command is this :

cd ../../../tests/src/main/test
ls
find . -name '*.vrlp' | while read FILENAME
do
    TEST_CASE=`echo $FILENAME | sed s/"\.\/"//g | sed s/"\.vrlp"//g | rev | cut -f1 -d"/" | rev`
    CLASS=`echo $FILENAME | sed s/"\.\/"//g | sed s/"\/$TEST_CASE"//g | sed s/"\.vrlp"//g`

The logic is inside a directory, filter all exists file by using find . -name ‘*.vrlp’, includes all sub-folders

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then retrieve the data into parameters.

TEST_CASE=`echo $FILENAME | sed s/"\.\/"//g | sed s/"\.vrlp"//g | rev | cut -f1 -d"/" | rev`
    CLASS=`echo $FILENAME | sed s/"\.\/"//g | sed s/"\/$TEST_CASE"//g | sed s/"\.vrlp"//g`

I tried something like follows, but I don’t know the sed command exactly doing and how to convert it into python script. for example

For retrieve the data send to parameter CLASS and TESTCASE , I did the cut (

cut -f1 -d"/" 

) but I don’t know how to do the sed and rev to retrieve the value for TEST_CASE and CLASS

for root, dirs, files in os.walk(../../../tests/src/main/test):
    for file in files:
        file_name = os.path.basename(file)
        file = os.path.splitext(file_name)
        if '.vrlp' in file:
            FILENAME = file[0] + file[1]
                TEST_CASE = FILENAME.split("/")[0] // how to apply sed and rev here?
                CLASS = FILENAME // how to apply sed here?

Any help will be much appreciated

>Solution :

The TEST_CASE= command is taking the value of FILENAME, removing the ./ prefix and the .vrlp suffix, reversing the order of characters, extracting the first field based on / as delimiter, and then reversing the order of characters again.

Then, the CLASS= command is taking the value of FILENAME, removing the ./ prefix, removing the /$TEST_CASE suffix, and removing the .vrlp suffix.

In python:

import os

os.chdir("../../../tests/src/main/test")
print(os.listdir())

for FILENAME in os.popen("find . -name '*.vrlp'"):
    FILENAME = FILENAME.strip()
    TEST_CASE = os.path.basename(FILENAME).replace(".vrlp", "")
    CLASS = os.path.dirname(FILENAME).replace("./", "").replace("/{}".format(TEST_CASE), "").replace(".vrlp", "")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading