sed and rev shell command into Python script

Question:

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

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

Asked By: BCNK

||

Answers:

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", "")

Answered By: akathimi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.