Using Python's string manipulation in Bash scripts

April 21, 2019

If you want to use Python's string manipulation for your bash scripts, it's easy. Step one is to play with strings in the Python interpreter, then put what you figured out in a line like the following:

# use python to create each line of text.
# the syntax where we have [:-3] removes the three letters of the
# suffix from the right side
for i in *; do echo "print 'ffmpeg -i $i $i'[:-3]+'mp4'"|python >> convert_script; done

# check the output
vim convert_script

# make it runnable
chmod u+x convert_script

# run it
./convert_script

As another example, here is using python to rename files where we replace the word "_new" with nothing - in effect, removing that text. The script adds the "mv" commands to a script called "rename.sh", which we then check to make sure it's good. Later, we make that script executable and run it.

for i in *; do echo "print 'mv $i ' + '$i'.replace('_new','')"|python >> rename.sh; done

Contact me at byronka (at) msn.com