0

I've not being able to find a post with this requirement. I'm using Python. I need to delete a line from a file only if next line starts with the characters "-----a".

If next line starts with spaces (or different characters) the line musn't be deleted.

Example:

-----arn:aws:iam::001122334455:policy/policy_name1 # delete this
-----arn:aws:iam::001122334455:policy/policy_name2 # delete this
-----arn:aws:iam::001122334455:policy/policy_name3 # delete this
-----arn:aws:iam::001122334455:policy/policy_name4 # delete this
-----arn:aws:iam::001122334455:policy/policy_name5 # do NOT delete this line
"Action": "s3:*", # do NOT delete this line either
-----arn:aws:iam::001122334455:policy/policy_name6 # delete this
-----arn:aws:iam::001122334455:policy/policy_name7 # delete this

I'm kinda new to Python and I'm being unable to find a way to do this. Any help will be much appreciated!

John Ingram
  • 164
  • 1
  • 12
Naxxio
  • 63
  • 1
  • 10
  • When you say "The same characters" do you mean always the specific characters "-----a", or the same N number of characters as on the current line? – John Ingram Mar 09 '22 at 13:24
  • the lines to be deleted always start with "-----a". But certain lines starting with "-----a" musn't be deleted if next line starts with a space or "Action" – Naxxio Mar 09 '22 at 13:26
  • I'd recommend reading the file from the bottom up and making note of lines that don't start with -----a, then you can skip the line that comes 'after' (above) them. Otherwise you could read the file and cache two lines, then add those two lines to a new file if they meet your condition. Another option would be to read the entire file once and make note of what lines need to remain, then go over the file again and apply those changes. – John Ingram Mar 09 '22 at 13:38
  • The first option sounds good. Could you add some example code to your answer so I can test it? – Naxxio Mar 09 '22 at 13:42
  • Let me take a look. When you say the file is huge, What do you mean? Gigabytes? Megabytes? – John Ingram Mar 09 '22 at 15:26
  • Not that big. The file is 5,2K – Naxxio Mar 09 '22 at 16:06
  • If it's that size you should be able to get away with Kris' approach. otherwise take a look at https://stackoverflow.com/questions/2301789/how-to-read-a-file-in-reverse-order – John Ingram Mar 09 '22 at 16:16

1 Answers1

1

If your file is not a huge one, then you can read and process it with a reverse analysis approach like below

lines = """-----arn:aws:iam::001122334455:policy/policy_name1
-----arn:aws:iam::001122334455:policy/policy_name2
-----arn:aws:iam::001122334455:policy/policy_name3
-----arn:aws:iam::001122334455:policy/policy_name4
-----arn:aws:iam::001122334455:policy/policy_name5
     "Action": "s3:*",     
-----arn:aws:iam::001122334455:policy/policy_name6
-----arn:aws:iam::001122334455:policy/policy_name7
""".splitlines()

# lines = open(fd).readlines() --> for a file!
selected_lines = []
found_qualified = False
for line in reversed(lines):
    if not line.startswith("-----a"):
        selected_lines.append(line)
        found_qualified = True
    elif found_qualified:
        selected_lines.append(line)
        found_qualified = False
selected_lines.reverse()
# Write selected lines to a file back!
print(selected_lines)

But , if you think the file is going to be huge, then you can follow a similar logic, but a different approach to split process the file.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • Thanks @Kris , but the file is huge, I failed to mention that in my post. How could I do it in that case? – Naxxio Mar 09 '22 at 13:45