0

I want to recursively replace the "blah blah blah" in my License region with nothing:

#region License
blah blah blah
blah blah blah
#endregion

should be replaced with

#region License
#endregion

This should apply to all of of my .cs files in a certain directory (recursive). I tried this with sed, but since I am on windows, I had some problems with line endings. How can I do this with perl (or python), or something native to windows?

EDIT: here is the solution I came up with, thanks to everyone here!:

#/bin/bash
list=`find . -name '*.cs' -o -name '*.h' -o -name '*.cpp'` 
for i in $list 
do
perl -i~ -ne 'if (/#region License/../#endregion/) {print if /#(?:end)?  region/;next};print' $i 
done
Jacko
  • 12,665
  • 18
  • 75
  • 126

7 Answers7

6

Something like this?

perl -i~ -pe 'undef $_ if /^#region License$/ .. /^#endregion$/'
choroba
  • 231,213
  • 25
  • 204
  • 289
1
#!/usr/bin/env python

with open('input') as fd:
    text=fd.read()

old="""#region License
blah blah blah
blah blah blah
#endregion

"""

new="""#region License
#endregion

"""

print text.replace(old,new)

add some os.walk to traverse the directory and either write the result to stdout or replace original file with new content. See e.g. https://stackoverflow.com/a/5421671/297323

Community
  • 1
  • 1
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 1
    nice "kiss" solution; and probably adequate so long as the license bodies are all exactly the same text. If there are a bunch of different license, this could be easily extended to replace each! – SingleNegationElimination Dec 07 '11 at 15:37
1
perl -ne 'if (/#region/../#endregion/) {print if /#(?:end)?region/;next};print' file

...which leave the "#region license" and "#endregion" lines in the output as requested.

JRFerguson
  • 7,426
  • 2
  • 32
  • 36
  • how do I get this to overwrite the old file with the changes? – Jacko Dec 07 '11 at 17:22
  • 1
    @Jacko : To perform an inplace edit, simply change the switches to: perl -i -ne ... If you want to preserve a copy of the original file (with a suffix) do: perl -i.old -ne ... – JRFerguson Dec 07 '11 at 17:36
  • 1
    THanks. This script doesn't seem to work if the file is not in the current directory. (I used find to enumerate all cs files) – Jacko Dec 07 '11 at 17:58
  • @Jacko : If necessary, specify an absolute path to your file. What "doesn't work"? How? – JRFerguson Dec 07 '11 at 18:02
  • actually, I needed to replace "#region" with #region License", or all regions would be deleted!! – Jacko Dec 07 '11 at 18:29
  • what about handling files where there is no #region License, and I want to add this text in ?? – Jacko May 16 '13 at 17:06
1

ex (vim -e) maybe is a good choice.

echo -e 'g/^#region License$/+1,/^#endregion$/-1d\nx' | ex program.cs

  • g/.../+1,/.../-1 => find lines between regexs(+1 => one line bellow, -1 => one line above)
  • d => delete
  • \n => Enter
  • x =>save and quit

In Windows, please use:

vim -c "g/^#region License/+1,/^#endregion/-1d" +x program.cs
kev
  • 155,172
  • 47
  • 273
  • 272
0

read lines of the file, go thru each line if line starts with #region start skipping next lines, if lines ends with #endregion start collecting lines again, output final line into file e.g.

def filter_lines(lines):
    newlines = []
    startmarker = '#region'
    endmarker = '#endregion'
    skip = False
    for line in lines:

        if line.startswith(startmarker):
            newlines.append(line)
            skip = True
            continue
        if line.endswith(endmarker):
            skip = False

        if not skip: newlines.append(line)

    return newli

nes

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
0

python, in case the license bodies are all different in all sorts of unpredictable ways:

#!/usr/bin/env python

with open('input') as fd:
    text=fd.read()

try:
    start, rest = text.split("#region License\n", 1)
    middle, end = rest.split("#endregion\n", 1)
    print "%s\#region License\n#endregion\n%s" % (start, end)
except ValueError:
    # didn't contain a properly formatted license:
    print text
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
0

I would do something like this:

perl -i.orig -0777 -p -e 's/#region License.*?#endregion/#region License\n#endregion/s' test.cc
  • -0777 means that the whole file will be slurped
  • -p is making the -e code to be surrounded by a while (<>) { ... print $_ } block
  • -i.orig does the editing in place, and creates a backup
  • s flag at the end of the substitution makes the .* in the regexp match over eol

Use find to determine which files to process

Sorin
  • 5,201
  • 2
  • 18
  • 45