To remove some lines from a file

U

umut.tabak

Dear all,

I am quite new to python. I would like to remove two lines from a file.
The file starts with some comments, the comment sign is $. The file
structure is

$
$
$
$
$
$
$
..
..
..
..
$
line1
line 2
$
$
..
..
..
$
actual commands

I would like to delete those two lines at the very beginning of the
file between two comment blocks. I have read things about python but my
library knowledge is still weak. So could you please help me with this
code.

I would like to call this as

myscript targetfile

I have read sth about command line arguments. Perhaps I can cope with
that myself but help is also appreciated on that.

Best regards,
 
J

Jerry

Very inelegant, but you get the idea:

counter = 0

f = open("test.txt")
for line in f.readlines():
if line[0] != "$" and counter < 2:
counter += 1
continue
else:
print line,
 
B

Bruno Desthuilliers

Dear all,

I am quite new to python. I would like to remove two lines from a file.

The canonical way to do so (at least for text files - which is the case
here) is :

open the source file in read mode
open a tmp file in write mode

for each line in source file:
if this line should be kept:
write it to the tmp file

close both files
replace source file with tmp file

(snip)
I would like to call this as

myscript targetfile

I have read sth about command line arguments. Perhaps I can cope with
that myself but help is also appreciated on that.

import sys
print sys.argv
 
S

Sebastian Busch

... I would like to remove two lines from a file.
...

I am quite new myself -- but wouldn't grep -v do that easier (and
perhaps faster)?

Greetings,
Sebastian.
 
S

Steve Holden

Sebastian said:
I am quite new myself -- but wouldn't grep -v do that easier (and
perhaps faster)?
Kindly show how to use grep to solve this problem:

Remove the first two lines that don't begin with "@" from a file.

regards
Steve
 
S

Sebastian Busch

Steve said:
... show ...

grep -v "`grep -v "commentsymbol" yourfile | head -2`" yourfile


i frankly admit that there is also 'head' invoved ;)

i really have no idea -- but i always thought that these coreutils and
colleagues do their jobs as fast as possible, in particular faster than
interpreted languages... however, as i posted last time, i was actually
not aware that you have to call three of them.

sebastian.
 
C

Chetan

Sebastian Busch said:
grep -v "`grep -v "commentsymbol" yourfile | head -2`" yourfile


i frankly admit that there is also 'head' invoved ;)

i really have no idea -- but i always thought that these coreutils and
colleagues do their jobs as fast as possible, in particular faster than
interpreted languages... however, as i posted last time, i was actually
not aware that you have to call three of them.

sebastian.
I don't have the original post to know exactly what is needed, but looks like
something that can be done by a single sed script.

-Chetan
 
S

Sebastian Busch

Chetan said:
I don't have the original post to know exactly what is needed, but looks like
something that can be done by a single sed script.

-Chetan

Hey!

The task is:

"Remove the first two lines that don't begin with "@" from a file."


Actually, the grep-thing I offered will also delete copies of these two
lines that occur in another place. That should be no problem if the file
is something like

@comment
deleteme
deleteme
@comment
data: x-y-dy

However, if this is not the case, it cannot be done this way.
How would you do it with sed?

Best,
Sebastian.
 
R

Roberto Bonvallet

Sebastian said:
The task is:

"Remove the first two lines that don't begin with "@" from a file."

awk 'BEGIN {c = 0} c < 2 && !/^@/ {c += 1; next} {print}' < mybeautifulfile
 
J

John Savage

Sebastian Busch said:
The task is:

"Remove the first two lines that don't begin with "@" from a file."

How would you do it with sed?

Why a sed solution in a python group?

sed '/^@/!{G;/\n\n\n/{P;d;};s/[^\n]*//;h;d;}' data
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top