I have this text file:
[admin]# cat /etc/passwd
root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
sys:!:3:3::/usr/sys:
adm:!:4:4::/var/adm:
uucp:!:5:5::/usr/lib/uucp:
guest:!:100:100::/home/guest:
nobody:!:4294967294:4294967294::/:
lpd:!:9:4294967294::/:
lp:*:11:11::/var/spool/lp:/bin/false
invscout:*:200:1::/var/adm/invscout:/usr/bin/ksh
nuucp:*:6:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
paul:!:201:1::/home/paul:/usr/bin/ksh
jdoe:*:202:1:John Doe:/home/jdoe:/usr/bin/ksh
and some code here
with open(file) as f2:
for lines in f2:
if "cat /etc/passwd" in lines:
for i in range(3):
cat = f2.readline()
print(cat)
if it finds the string "cat /etc/passwd"
, it will store the next couple of lines inside the variable cat
output:
root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
This is the case if I call cat
from inside the for loop. If I call it outside of the for loop, I only get the last line :
bin:!:2:2::/bin:
I assume the line for i in range(3)
is the reason for this. is there a way I can call cat
outside the loops and have it return each of the lines I want printed?