Shweta
Shweta

Reputation: 5446

Deleting last line of a file

I want to delete last line of a file on a particular condition. I used sed and cut but they work only on the output stream but I want to do this in the file. Can someone help me? Thanks in advance.

Upvotes: 14

Views: 12148

Answers (3)

Shad
Shad

Reputation: 15451

sed -ie '$d' filename.txt

The i option tells sed to edit the file in place.

Upvotes: 24

darklion
darklion

Reputation: 1055

I'll use a variant on another answer and suggest a simple use of ed:

ed tmp.tmp << EOC
$
d
w
q
EOC

Otherwise you can use sed/cut to a temporary file and mv to overwrite the starting file.

Upvotes: 1

alcoholtech
alcoholtech

Reputation: 300

You can always do:

cat * > out

Where you could remove 'cat *' with whatever you're doing, and the out is the result of that command.

I can take a more thorough look if you provide more information on what you're trying to do.

Upvotes: 0

Related Questions