23 jul 2023

find & replace expressions inside a text file running a simple python script in GNU-Linux terminal

FILE 1 (BEGIN)

readMe.txt

································································································

Hi!

This set of files is just a little experiment to do some work over a file with a python script.

File "example.txt" contains expressions that will be replaced with new ones as described on “findAndReplace.py” file in this folder. (there is an “.example.txt” file to have it back again in case you want to. Just make it visible and take the “*.hackUp” fake extension).

See it in action by running the “doAll.sh” batch file as root. Remember to “chmod 755 file2change” to the files if you find it suitable. To install stuff you’ll need internet (o_o)

have fun!

(=

································································································

FILE 1 (END); FILE 2 (BEGIN)

findAndReplace.py

································································································

from pathlib2 import Path
# import some libraries
path = Path("example.txt")                  
# file located in the same folder. tune to your desired route, like /home/user/whatever
text = path.read_text()
# read the file
text = text.replace("=)", ":)")
# replace whatever is necessary
path.write_text(text)
# and finally overwrite the file.

# I had to install...       apt-get install python-pip python3-pip -Y
# & after alsa ...          pip install pathlib2 -Y
# due to                    module not loadad error
# ;-)

# do run from terminal...   python findAndReplace.py


# CC Share alike too the latest! ONLY FOR EDUCATIONAL MEANS!!

································································································

FILE 2 (END); FILE 3 (BEGIN) 

example.txt

································································································

=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)

································································································

FILE 3 (END); FILE 4 (BEGIN)

.example.txt.backUp 

································································································

=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)
 

································································································

FILE 4 (END); FILE 5 (BEGIN)

doAll.sh

································································································

#!/bin/bash

apt-get update

apt-get upgrade -y

apt-get install python -y

apt-get install python-pip python3-pip -y

pip install pathlib2

python findAndReplace.py

································································································

FILE 5 (END)

 

# pseudocode is not a crime!


 

No hay comentarios:

Publicar un comentario