Bash or Python to go backwards?
Hi,
I have a text file which a lot of random ocurrences 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 ocurrence of a line starting with the same string, like @STRING_A @STRING_A and if it happens, to delete the ocurrence 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 |
In principle, this should be possible with any language....
But I would not think of it as "going backwards"---I would think the pseudo-code would go something like this: Code:
loop: |
|
I don't get the difference between forward and backward in this case: if the two string at the beginning of the line are the same and they are separated by three other lines, I don't see any difference by going up or down.
Anyway, if you want to parse a file from the end to the beginning, consider the tac command to completely reverse the content of the file. Once you've parsed/modified the file, tac it again and the trick is done. |
Quote:
Code:
#!/usr/bin/env python Code:
# more file |
All times are GMT -5. The time now is 03:11 AM. |