LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2017, 08:28 AM   #1
bindo
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Rep: Reputation: Disabled
python deleting a certain number of lines from a file given a keyword


Newbie here and I'd really appreciate your help in order for me to determine whats the best way to go with below scenario. Please note I'm using python 2.7.X

I've got a file "file.txt" in a centos 7 box which has following lines in it. (Pay attention to the new lines)

Code:
blah blah blah

ID:name1:1bj409ju9
how are you

Im good 100
blah blah

ID:name2:987krjtu
not so good

too bad 900
blah blah

some words blah blah
As you notice the lines start with "ID" have a pattern. My attempt is to search for ID:name[x] and delete 5 lines (including the space). for an instance say i want to delete below set of lines from the file.
Code:
ID:name1:10.1.1.10
how are you

I'm good 100
blah blah
I tried the following code but it deletes only the lines matching "somename1". Please advice.

Code:
#!/usr/bin/python
import fileinput

filename = r"file.txt"
counter = -1
for linenum,line in enumerate(fileinput.FileInput(filename, inplace=1)):
    if "name1" in line:
        counter = linenum + 6
        if linenum == counter:
            line.strip()
    else:
        print line,
Please note that I want to get rid of the new empty line in between "blah blah" and "ID:name2:987krjtu". Please help.
 
Old 04-26-2017, 12:04 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
does it have to be python? to me it sounds like a perfect use case for sed.
https://duckduckgo.com/?q=sed+delete...lowing+pattern
 
1 members found this post helpful.
Old 04-26-2017, 12:08 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,925

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
if you want to do it with python take care about your counter and linenum.
I mean you need to read all the lines, but you need to skip some of them, but the implemented logic does not really take care about skipping...
 
Old 04-28-2017, 08:09 PM   #4
bindo
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
@ondoho,

Thanks so much. Eventhough I prefer to do it within python what you've suggested works a huge deal too.
 
  


Reply



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
[SOLVED] Deleting lines from a file in a bash script MrTwister Linux - General 3 01-27-2014 07:30 AM
conditionally deleting lines from file vaibhav_soham Linux - General 1 03-10-2011 05:45 AM
Deleting multiple lines above and below an expression in a file Ransak Programming 7 05-20-2010 08:08 PM
Deleting lines from a file horacioemilio Programming 16 12-18-2007 06:07 PM
deleting specified lines in a huge text file ruh31 Linux - General 10 06-30-2006 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 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
Open Source Consulting | Domain Registration