473,748 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete lines containing a specific word

Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?

f=open("output. pdb", "r")
for line in f:
line=line.rstri p()
if line:
print line
f.close()

If python in Linux accepts lines beginning with # as comment lines, please also
a script to comment lines containing a specific word, or words, and back, to
remove #.

Thanks
francesco pietra
_______________ _______________ _______________ _______________ _______________ _________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
Jan 6 '08 #1
2 19751
On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:
Please, how to adapt the following script (to delete blank lines) to
delete lines containing a specific word, or words?
That's tricky, because deleting lines from a file isn't a simple
operation. No operating system I know of (Windows, Linux, OS X) has a
"delete line" function.

Do you really need to delete the lines in place? It would be much simpler
to leave the original data as-is, and create a new file with just the
lines that aren't deleted.

f=open("output. pdb", "r")
for line in f:
line=line.rstri p()
if line:
print line
f.close()
How to adapt this script:

First, think about what this script does. That is, it goes through each
line, and if the line is not blank, it prints it.

What do you want it to do instead? You want it to print the line if the
line doesn't contain a specific word. So that's the first thing you need
to change.

Secondly, you might want the script to write its output to a file,
instead of printing. So, instead of the line "print line", you want it to
write to a file.

Before you can write to a file, you need to open it. So you will need to
open another file: you will have two files open, one for input and one
for output. And you will need to close them both when you are finished.

Does that help you to adapt the script?

If python in Linux accepts lines beginning with # as comment lines,
please also a script to comment lines containing a specific word, or
words, and back, to remove #.
The same process applies. Instead of "delete line", you want to "comment
line".

--
Steven
Jan 6 '08 #2
On Jan 7, 1:21 am, Francesco Pietra <chiendar...@ya hoo.comwrote:
Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?

f=open("output. pdb", "r")
for line in f:
line=line.rstri p()
if line:
print line
f.close()

If python in Linux accepts lines beginning with # as comment lines, please also
a script to comment lines containing a specific word, or words, and back, to
remove #.

Thanks
francesco pietra

_______________ _______________ _______________ _______________ _______________ _________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
for line in open("file"):
if not "word" in line:
print line
on the command, use the ">" operator to redirect to a file
Jan 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3303
by: mahesr | last post by:
Frds, how to crawl a specific word in whole website using php.need which page it has Example: want to crawl a 144 items found,on somepage.html. Pls if u have code pls mail email address removed- Ronald
2
1737
by: lekshminair | last post by:
U]can u solve it I am trying to take a specific word in textfile. for example: new .txt "Please follow these guidelines when posting questions. Submitting clear and concise questions allows those reading to understand your problem and respond easily." I this text file extract with "ing"formatt words ie,posting submitting
8
4201
by: Umesh | last post by:
Please help. THANKS.
0
1051
by: AcidBurn | last post by:
Hi Can anyone help me ragarding my query Im trying to develop a classic asp script that will count the number of lines of ms word document. say...this is the content of the word file: Line1
3
3193
by: Robertf987 | last post by:
Well, I think I've described what I want to do in the title here. In the database, I have two main tables that contain the main data for the database. One for group expenditures, another for application details. I have a query for each of them to copy all the contents of the table into a new table, then delete all the data from the main table. I need to do this becasue once a year the data needs to be removed, ready for putting in new data. I...
2
2527
by: Kristapins | last post by:
Hello! Can anyone help me... I have *.txt file containing lines like... 100 0 0 7 high UNLOCKED CRITICAL Door open in site CLEARED 100 11 0 7 low UNLOCKED CRITICAL Door Open CLEARED 100 12 0 0 high UNLOCKED CRITICAL Power suppy CLEARED 100 12 0 1 high UNLOCKED CRITICAL Fire CLEARED 100 12 0 2 low UNLOCKED CRITICAL Temperature CLEARED Code is...
0
2083
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT 446 27.436 34.037 56.145 1.00 0.00 H HETATM 7009 H2 WAT 446 27.049 33.827 54.563 1.00 0.00 H occurring in a 300MB file. In present case each three-lines block is followed by line renumbering (7007,
2
2571
by: weldeslasai | last post by:
I need help on preparation of c++ code that could serve as for finding specific word from.In other words the code for very simple dictionary code. Thanks
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.