0

In a list of textfiles I need to replace a token with the name of the file.

Is there an easy way to do this? The prefered tool for me would be notepad++, but grep, python, powershell or other other ways are also ok. I work on windows.

Example:

filenames

contact.aspx

default.aspx

content first file

Hallo <%= Html.Resource("Title") %>

content second file

Whats up <%= Html.Resource("Title") %>

The required result is:

first file

Hallo <%= Resource.contact_aspx.Title %>

second file

Whats up <%= Resource.default_aspx.Title %>

I dont need the complete solution here: just the part that lets me use the filename in a replace statement would get me started.

Mathias F
  • 15,906
  • 22
  • 89
  • 159

5 Answers5

1

You could use the fileinput module. It feels a little awkward to have some module redirect stdout, but it should work like this:

#usage: python thisscript.py token file file2 file3
import sys
from fileinput import input
token = sys.argv[1]
file_input = input(sys.argv[2:], inplace=True)
for line in file_input:
    print line.replace(token, file_input.filename())
tback
  • 11,138
  • 7
  • 47
  • 71
0

If you can work with awk then this might help -

awk -v FS="%" -v OFS="%" '
/Html.Resource\("Title"\)/{sub(/.*/,"= Resource."FILENAME".Title ",$2); print;next}1
' Input_File

The above one-liner sets the Field Separator and Output Field Separator to %. Looks for lines that contains the pattern /Html.Resource\("Title"\)/. If it finds it, it runs the action which is substituting the $2 column with FILENAME (which is an built-in variable that holds the name of the file) and prints it and moves to the next line. 1 is for lines that do not contain the pattern and get printed as is.

Test:

[jaypal:~/Temp] cat default.aspx 
Whats up <%= Html.Resource("Title") %>

[jaypal:~/Temp] awk -v FS="%" -v OFS="%" '
> /Html.Resource\("Title"\)/{sub(/.*/,"= Resource."FILENAME".Title ",$2); print;next}1
> ' default.aspx
Whats up <%= Resource.default.aspx.Title %>

[jaypal:~/Temp] cat contact.aspx 
Hallo <%= Html.Resource("Title") %>

[jaypal:~/Temp] awk -v FS="%" -v OFS="%" '
> /Html.Resource\("Title"\)/{sub(/.*/,"= Resource."FILENAME".Title ",$2); print;next}1
> ' contact.aspx 
Hallo <%= Resource.contact.aspx.Title %>
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

I borrowed from: https://stackoverflow.com/a/39110/1104941

Save this script as a .py file. Place it in the same directory as the .aspx files that you want modified (obviously keep the originals in a safe place).

import os
from tempfile import mkstemp
from shutil import move

dir_list=os.listdir('.')
for fname in dir_list:
    if fname.split('.')[1] == 'aspx':
        #Create temp file
        fh, abs_path = mkstemp()
        new_file = open(abs_path,'w')
        old_file = open(fname, 'r')
        for line in old_file:
            if '<%= Html.Resource("Title") %>' in line:
                new_line = line.replace('<%= Html.Resource("Title") %>', '<%= Resource.' + '_'.join(fname.split('.')) + '.Title %>')
                new_file.write(new_line)
            else:
                new_file.write(line)    
        #close temp file
        new_file.close()
        os.close(fh)
        old_file.close()
        #Remove original file
        os.remove(fname)
        #Move new file
        move(abs_path, fname)
Community
  • 1
  • 1
sgallen
  • 2,079
  • 13
  • 10
0

The following regex will generically replace Html.Resource, and you can use python's open(...) and os.path utils for traversing and keeping track of filenames for use in second argument of sub. And so, src would be the contents of the file that you would write back out from re.sub return

import re

fn = 'contact_aspx'
src = '<%= Html.Resource("Title") %>'

re.sub(r'<%= Html.Resource\("(.*)"\) %>', \
    r'<%= Resource.{0}.\1 %>'.format(fn), \
    src)

# outputs
# '<%= Resource.contact_aspx.Title %>'
dskinner
  • 10,527
  • 3
  • 34
  • 43
0

Here's a solution in Powershell:

(Get-Content -Path contact.aspx) -replace '(Hallo\s?<%=\s?)(.+?)(\s?%>)', '$1Resource.contact_aspx.Title$3' | Set-Content -Path contact.aspx

(Get-Content -Path default.aspx) -replace '(Whats\s?up\s?<%=\s?)(.+?)(\s?%>)', '$1Resource.default_aspx.Title$3' | Set-Content -Path default.aspx
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124