Calling `sed` from inside a python program
Question: I have the following code snippet in bash which I use for processing a JSON
file.I want to do the exactly same functionality in python. Can I call `sed`
from python? Or how do I achieve the same thing.

    
    
    for file in /home/Work/ts_parts/*
    do
            event=$(cat $file | jq '.Event')
            name="$(basename "$file")"
            if [[ "$event" =~ "aaa.bbb.ccc.dddd" ]]  || [[ "$event" =~ "eeee.ffff.gggg.hhhh" ]]; then
                    echo "Coming Here"
                    jq ".url = \"$(jq '.Msg' $file | sed 's/.*proxy=\([^&]*\).*/\1/')\"" $file  > ts_parts/out_$name
            else
                    jq ".url = null" $file > ts_parts/out_$name
            fi
            rm -f $file
    done
    


Answer: You could use regular expressions:

    
    
    import re
    
    s = "abc proxy=123&456"                 # Test string
    
    mat = re.search(r'proxy=([^&]*)', s)
    print(mat.group(1))                     # 123
    

This code only does what your sed replacement (extraction) does. Anything else
from the script you'd also have to implement, or call programs directly.

