Here is the script to replace all strings in a file.
Replace Strings In A File
#!/usr/bin/python#Created by Leonimportos,glob,reold="oldfile"new="newfile"fin1=open(old)fout=open(newfile,"w")data=fin1.readlines()forlineindata:fout.write(line)m=re.search(r"some string",line)ifm:printm.group(1)fout.write("some new strings")fin1.close()fout.close()
You can also use the sed command:
sed -i .bk 's/OLD_STRING/NEW_STRING/g' FILE_NAME
As on some system, sed is at a very old version, and some useful characters cannot be recognized. In this case, you can use Perl as an alternative: