Hello,

I have file name is file.txt inside it this lines

line1=hello world
line2=hello web
line3=hello egy


I want print full lines start with line2=

like in linux command
cat file.txt | grep "line2="
this code will print full line --> line2=hello web

And if any one have books in python like this techniques tell me

Thanks

Recommended Answers

All 4 Replies

import os
os.system( 'cat file.txt | grep "line2="' )

Sometimes the simplest solution is the best one.

commented: indeed +6

>Sometimes the simplest solution is the best one.
Sometimes simplest solution is not the portable one. Your solution lean on grep to be available.

f=open('file.txt');
lines=f.readlines();
for l in lines:
# remove .lstrip() from the following next line, if you don't want to be strict. 
# Any whitespace wont stripped for detection.
    if l.lstrip().startswith("line2="): 
        print l

f.close()

Thanks jlm699 but i know that and i dont like this style

siddhant3s thanks very must that is was i want it

I was use while True with startswith but the resulte was TRUE or FALSE not the full line

But if i want search about works didn't start the line like "hello web" and want to print full line "line2=hello web"

what the technique ?

The techniques are:
* RTFM & STFW
* Learn to frame and type english
* Try to make more meaning full thread titles
* And at last but not the least read this

Don't take it personally, but it just don't make sense to keep asking trivial questions.
PS: Hint, use the str.find() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.