-1

I have a notepad++ file with contents like below

{s:11:"wpseo_title";s:42:"Web Designing training institutes in Kochi";}i:357;a:1:
{s:11:"wpseo_title";s:32:"CSS training institutes in Kochi";}i:358;a:1:
{s:11:"wpseo_title";s:34:"HTML5 training institutes in Kochi";}i:359;a:1:
{s:11:"wpseo_title";s:39:"JavaScript training institutes in Kochi";}i:360;a:1:
{s:11:"wpseo_title";s:32:"XML training institutes in Kochi";}}}

I need a way to search for the phrase ";s:42:" and increment the number part of the phrase by 1. In this case, 42 will become 43.

I just need to do it. Dont care if it is through a python script like this

Notepad++ Regular Expression add up numbers

or any other method.

Please help me. I am new to python/ any such language.

Community
  • 1
  • 1

4 Answers4

1

Perl one-liner version:

perl -ne 's/(?<=;s:)(\d+)(?=:)/$1+1/ge; print' data.txt
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • Can you tell me where to run this – user3290168 Apr 30 '14 at 18:09
  • I executed it in online perl and this is the error Bareword found where operator expected at main.pl line 1, near "'s/(?<=;s:)(\d+)(?=:)/$1+1/ge; print' input" (Missing operator before input?) syntax error at main.pl line 1, near "-ne" Execution of main.pl aborted due to compilation errors. – user3290168 Apr 30 '14 at 18:12
  • Where does that `input` come from? – Lee Duhem Apr 30 '14 at 18:19
0
target_file = "some_file.txt"
with open("tmp_out.txt","w") as f_out:
   with open(target_file) as f_in:
        for line in f_in:
            f_out.write(re.sub(";s:(\d+):",lambda match:";s:%d:"%(int(match.groups(0)[0])+1,line) + "\n")
shutil.move("tmp_out.txt",target_file)

something like that I think

or even better

import fileinput

for line in fileinput.input(target_file, inplace=True):
    print re.sub(";s:(\d+):",lambda match:";s:%d:"%(int(match.groups(0)[0])+1,line)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Using your link as an example, the regex match should be:

def calculate(linenumber, match):
    editor.pyreplace(match.group(0), ';s:%d="%d"' % (match.group(1), str(int(match.group(2))+1)), 0, 0, linenumber, linenumber)

editor.pysearch(r';s:([0-9])="([0-9]+)"', calculate)

I think. I've never actually done this before!

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Though, one liner in Python is difficult to achieve, but you can use something similar using the callback feature of re.sub

repl = lambda e: e.group(1) + str(int(e.group(2))+1)
with open("in.txt") as fin:
    with open("out.txt","w") as fout:
        fout.write(re.sub(r"(;s:)(\d+):",repl, fin.read()))
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Semicolon seems to be missing at main.pl line 1. Bareword found where operator expected at main.pl line 2, near ") as" (Missing operator before as?) Bareword found where operator expected at main.pl line 3, near ") as" (Missing operator before as?) Can't modify constant item in scalar assignment at main.pl line 1, near "lambda e:" syntax error at main.pl line 1, near "lambda e:" Illegal declaration of anonymous subroutine at mai – user3290168 Apr 30 '14 at 18:18
  • @user3290168: This is Python not Perl – Abhijit Apr 30 '14 at 18:24