Replace old print function in python with new one in sublime text - replace

So I found a script on github that i wanted to use but the problem was that it was written using an older version of python therefore the printing function was written as print 'Hello World!' instead of print('Hello World!') and I coulnd't execute it, so I wanted to know if there's a way of replacing every line of the script that has the old python function with the new one using sublime text 3. I thought of trying:
Find: print '(.*)' - to select every print function which it works but when I try to replace it with
Replace: print("(.*)") - it simply replaces what's inside of the print function with (.*) instead of the actual content

Using Sublime:
Find: print ('.*')
Replace: print\(\1\)
Before:
After:

Related

Python 2.x find and replace multi line text [duplicate]

This question already has answers here:
replacing text in a file with Python
(7 answers)
Closed 8 years ago.
I know that many questions exist here on finding and replacing text in file using python 2. However being a very new to python, I did not understand the syntax and may be the purpose also will be different.
I am looking for something very simple code lines as in linux shellscript
sed -i 's/find/replace/' *.txt
sed -i 's/find2/replace2/' *.txt
Can this code work to replace a multiline text
with open('file.txt', 'w') as out_file:
out_file.write(replace_all('old text 1', 'new text 1'))
out_file.write(replace_all('old text 2', 'new text 2'))
Also, there seems a problem with getting another newline, which I do not want. Any ideas or help?
So, with Python, the easiest thing to do is read all the text from the file into a string. Then perform any necessary replacements using that string. Then write the entire thing back out to the same file:
filename = 'test.txt'
with open(filename, 'r') as f:
text = f.read()
text = text.replace('Hello', 'Goodbye')
text = text.replace('name', 'nom')
with open(filename, 'w') as f:
f.write(text)
The replace method works on any string and replaces any (case-sensitive) match of the first argument with the second. You're reading and writing to the same file, just in two different steps.
Here is a quick sample. If you want more powerful search/replace you can use regex instead of string.replace
import fileinput
for line in fileinput.input(inplace=True):
newline = line.replace('old text','new text').strip()
print newline
Put the above code in a desired file, say sample.py, and assuming your python is in your path you can run as:
python sample.py inputfile
That will replace 'old text' with 'new text' in inputfile. Ofcourse you can pass multiple files as arguments as well. See https://docs.python.org/2/library/fileinput.html

Remove all lines that don't end with specific string

working on a large text file and I'd like to remove all lines that don't contain the text "event":"click"}]
I've tried to do some regex within Sublime 3 and can't get it to stick.
I have not used sublime but you could select all line not containing the text "event":"click"}] with the regex:
^(.(?!"event":"click"\}\]))*$
I think you could replace them by nothing(empty string) or backspace
Use this one to get result to stdout
sed -n '/"event":"click"\}\]$/p' your_large_file
Use this one to keep only lines that end with "event":"click"}], your_large_file.old backup will be generated
sed -i.old -n '/"event":"click"\}\]$/p' your_large_file

Using sed to replace one line (that might change) with another

I want to run a script that changes a line in the HTML code, indicating when the page was last updated. So for instance, I have the line
<d>This page was last updated on 29.04.2013 at 00:34 UTC</d>
and I am updating it now, so I want to replace that line with
<d>This page was last updated on 15.05.2013 at 15:50 UTC</d>
This is the only line in my source code that has the <d> tag, so hopefully that helps. I already have some code that generates the new string with the current date and time, but I can't figure out a way to replace the old one (which changes, so I don't know exactly what it is).
I've tried putting in a comment <!--date--> in the previous line, deleting the whole line that has <d> (with grep), and then putting in a new line after the comment that is the new string, but that fails. For example, if I want to just insert the string text after the comment, and use
sed -i 's/<!--date-->/<!--date-->text/' file.html
I get invalid command code j. I think it might be because there are some special characters like <,!, and > in the strings, but if I want to put in the date string above, I will have even more, like : and /. Thanks for any ideas on how to fix this.
This will change the text only on lines that contain <d>:
sed -i.bak "/<d>/s/on .* at [^<]*/on newdate at newtime/" file.html
I've tested this with the BSD sed that ships with MacOS X 10.8.3
You don't need your <!--date--> hack. You can use regular expressions and another delimiter besides "/" in your sed command:
sed -i.bak 's#<d>This page was last updated on.*</d>#<d>This page was last updated on 12.05.2013 at 00:38 UTC</d>#' whatever.html
Or, if you have your update in a variable called $replacement:
sed -i.bak "s#<d>This page was last updated on.*</d>#$replacement#" whatever.html
When using the command line, try escaping special characters like this:
! ===> \!

Perl regex extracting a match using braces

I tested the following code
#! /usr/bin/perl
use strict;
use English;
#this code extracts the current scripts filename
#by removing the path from the filepath
my $Script_Name = $PROGRAM_NAME;
${Script_Name} =~ s/^.*\\//; #windows path
#${Script_Name} =~ s/^.*\///; #Unix based path
print $Script_Name;
and i don't understand why these braces extract the match without using a /r modifier. can anyone explain why and how this works or point me to some documentation?
You're getting a little confused!
The braces make no difference. ${Script_Name} is identical to $Script_Name.
You code first copies the entire path to the script file from $PROGRAM_NAME to $Script_Name.
Then the substitution removes everything up to and including the last backslash, leaving just the file name.
The /r modifier would be used if you wanted to modify one string and put the result of the modification into another, so you could write your code in one step as
$Script_Name = $PROGRAM_NAME=~ s/^.*\\//r

How can I remove the text before and after a particular character?

I have been trying to remove the text before and after a particular character in each line of a text. It would be very hard to do manually since it contain 5000 lines and I need to remove text before that keyword in each line. Any software that could do it, would be great or any Perl scripts that could run on Windows. I run Perl scripts in ActivePerl, so scripts that could do this and run on ActivePerl would be helpful.
Thanks
I'd use this:
$text =~ s/ .*? (keyword) .* /$1/gx;
You don't need software, you can make this part of your existing script. Multiline regex replace along the lines of /a(b)c/ then you can backref b in the replacer with $1. Without knowing more about the text you're working with it's hard to guess what the actual pattern would be.
Presuming that you have the following:
text1 text2 keyword text3 text4 text5 keyword text6 text7
and what you want is
s/.*?keyword(.*?)keyword.*/keyword$1keyword/;
otherwise you can just replace the whole line with keyword
An example of the data may help us be clearer
I'd say, that if $text contains your whole text, you can do :
$text =~ s/^.*(keyword1|keyword2).*$/$1/m;
The m modifier makes ^ and $ see a beginning and an ending of line, and not the beginning and ending of the string.
Assuming you want to remove all text to the left of keyword1 and all text to the right of keyword2:
while (<>) {
s/.*(keyword1)/$1/;
s/(keyword2).*/$1/;
print;
}
Put this into a perl script and run it like this:
fix.pl original.txt > new.txt
Or if you just want to do this inplace, perhaps on several files at once:
perl -i.bak -pe 's/.*(keyword1)/$1/; s/(keyword2).*/$1/;' original.txt original2.txt
This will do inplace editing, renaming the original to have a .bak extension, use an implicit while-loop with print and execute the search and replace pattern before each print.
To be safe, verify it without the -i option first, or at the very least on only one file...