LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash or Python to go backwards? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-or-python-to-go-backwards-733892/)

horacioemilio 06-18-2009 07:56 AM

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

pixellany 06-18-2009 08:09 AM

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:
    look for <pattern>
    if found, then:
        note line number in a variable (eg DELNUM)
        look ahead for next occurence
        if found, break out of main loop (with a flag set saying we should delete first pattern)
        if not, break out into main loop (keep looking for <pattern>
    end if
end loop
if flag
    re-read the file and delete line # $DELNUM
    else exit
end if


H_TeXMeX_H 06-18-2009 08:20 AM

I say you use awk or perl.
Here's a good awk tutorial:
http://www.grymoire.com/Unix/Awk.html

colucix 06-18-2009 08:33 AM

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.

ghostdog74 06-18-2009 08:42 AM

Quote:

Originally Posted by horacioemilio (Post 3578298)
What do you think ? Is it possible to do it in bash or python ?
Thanks

forget bash. Here's a Python sample
Code:

#!/usr/bin/env python
s=[]
for line in open("file"):
    line=line.strip()
    if line.startswith("@STRING_A"):
        for item in  s[-3:]:
            if "@STRING_A" in item:               
                print '\n'.join(s[:-3])
                print '\n'.join(s[-3:]).replace("@STRING_A","")
                s=[]
                break               
    s.append(line)

output
Code:

# more file
1. this is a line
2. this is a line
@STRING_A this is a line
4. this is a line
5. this is a line
@STRING_A this is a line
6. ......
7. this is a line
8. this is a line
9. this is a line
@STRING_A this is a last line
10. this is a line
11. this is a line
@STRING_A this is a last line

# ./test.py
1. this is a line
2. this is a line
 this is a line
4. this is a line
5. this is a line
@STRING_A this is a line
6. ......
7. this is a line
8. this is a line
9. this is a line
 this is a last line
10. this is a line
11. this is a line

this method makes use of arrays also, but it won't fill up till the end of the file, as its cleared every time your string is found. the last @STRING_A is missing, i leave to you to fix that. :)


All times are GMT -5. The time now is 03:11 AM.