-2

Below fastfile (more than 1000 lines) I would like to search for string "Validate repo test2" and delete lines starting from "Validate repo test2" upto string "end" and rewrite content to new file.

Fastfile

desc "Validate repo test1"
lane :validate_repo do
lint_source
execute_tests
validate_docs
ensure_tool_name_formatting
ensure_code_samples
ensure_special_docs_code_samples
ensure_code_snippets
ensure_actions_config_items_formatting
end

desc "Validate repo test2"
lane :validate_repo do
lint_source
execute_tests
validate_docs
ensure_tool_name_formatting
ensure_code_samples
ensure_special_docs_code_samples
ensure_code_snippets
ensure_actions_config_items_formatting
end

desc "Validate repo test3"
lane :validate_repo do
lint_source
execute_tests
validate_docs
ensure_tool_name_formatting
ensure_code_samples
ensure_special_docs_code_samples
ensure_code_snippets
ensure_actions_config_items_formatting
end

rag4260
  • 1
  • 2

3 Answers3

1

You could do something like this:

with open('Fastfile', 'r') as f_orig, open('Fastfile_new', 'w') as f_new:
    skipping = False
    for line in f_orig:
        if 'Validate repo test2' in line:
            skipping = True
        if not skipping:
            f_new.write(line)
        if line[:3] == 'end':
            skipping = False
mttpgn
  • 327
  • 6
  • 17
  • @mttpgn..Thank you! what does this line do "line[:3]" exactly – rag4260 Mar 28 '19 at 02:20
  • @rag4260 `line[:3]` is a slice index and means the first three characters of the line. Hence, every line gets checked whether the first three characters are 'end'. Search for "slice" on this page for more details: https://docs.python.org/3/tutorial/introduction.html – mttpgn Mar 28 '19 at 02:26
  • i get an extra line after removing the lines upto string “end”...how can we remove that line – rag4260 Mar 28 '19 at 21:44
0

Maybe there are many solutions, but I think the follow codes can solve your problem too.

need_delete = False
with open(path_to_old_file, 'r') as fin, open(path_to_new_file, 'w+') as fout :
    for line in fin:
        if line.endswith('"Validate repo test2"\n'):
            need_delete = True
        if need_delete and not line.strip():
            need_delete = False
            continue
        if not need_delete:
            fout.write(line)

I hope this will help you.

FarmerLi
  • 330
  • 1
  • 3
  • 12
  • i get an empty line after removing the lines upto string “end”...how can we remove that line.. i tired using readline() inside if block where we are checking for string “end” but it did not work out – rag4260 Mar 28 '19 at 22:26
  • So you want to delete to the empty line. Replace "line == 'end\n'" with "not line.strip()" will make it. – FarmerLi Mar 29 '19 at 03:30
0

I'm new to this, so I'm not sure how to credit the author, but this was useful to me: Regex Match all characters between two strings Thanks @zx81

You can use the regex:

(?s)(?<="Validate repo test[\d]*").*(?=end)

http://www.rexegg.com/regex-modifiers.html#dotall The first section will enable "dot all mode", the rest of the regex says "Selects all characters between ""Validate repo test[\d]*"" and "end"". From there you can use regex sub to remove all of them. All together it would look a bit like this:

import re

fileText = file.read()
regex = re.compile(r"\"Validate repo test[\d]*\"", re.DOTALL)
result = re.sub(regex, "", fileText)

file.write(result)
EnigmaticBacon
  • 353
  • 6
  • 21