Bash or Python to go backwards?
Question: I have a text file which a lot of random occurrences of the string @STRING_A,
and I would be interested in writing a short script which removes only some of
them. Particularly one that scans the file and once it finds a line which
starts with this string like

    
    
    @STRING_A
    

then checks if 3 lines backwards there is another occurrence of a line
starting with the same string, like

    
    
    @STRING_A
    
    
    @STRING_A
    

and if it happens, to delete the occurrence 3 lines backward. I was thinking
about bash, but I do not know how to "go backwards" with it. So I am sure that
this is not possible with bash. I also thought about python, but then I should
store all information in memory in order to go backwards and then, for long
files it would be unfeasible.

What do you think? Is it possible to do it in bash or python?

Thanks


Answer: Funny that after all these hours nobody's yet given a solution to the problem
as actually phrased (as @John Machin points out in a comment) -- remove just
the leading marker (if followed by another such marker 3 lines down), not the
whole line containing it. It's not hard, of course -- here's a tiny mod as
needed of @truppo's fun solution, for example:

    
    
    from itertools import izip, chain
    f = "foo.txt"
    for third, line in izip(chain("   ", open(f)), open(f)):
        if third.startswith("@STRING_A") and line.startswith("@STRING_A"):
            line = line[len("@STRING_A"):]
        print line,
    

Of course, in real life, one would use an `iterator.tee` instead of reading
the file twice, have this code in a function, not repeat the marker constant
endlessly, &c;-).

