LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-18-2009, 08:56 AM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
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
 
Old 06-18-2009, 09:09 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 06-18-2009, 09:20 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
I say you use awk or perl.
Here's a good awk tutorial:
http://www.grymoire.com/Unix/Awk.html
 
Old 06-18-2009, 09:33 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 06-18-2009, 09:42 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by horacioemilio View Post
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.

Last edited by ghostdog74; 06-18-2009 at 09:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: python: command not found blueheron66 Programming 12 01-09-2009 04:16 PM
LXer: Python 3 changes breaks backwards compatibility LXer Syndicated Linux News 0 12-22-2008 05:30 PM
bash vs perl vs python DJOtaku Programming 11 02-07-2008 07:21 AM
bash vs perl vs python keceli Programming 11 07-23-2007 11:57 PM
bash, perl, or python? s_siouris Linux - Software 11 05-13-2005 04:15 PM


All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration