[Last Call] Learn how to a build a cloud-first strategyRegister Now


Remove Last Line Of File

Posted on 2006-05-05
Medium Priority
Last Modified: 2010-04-21
Hi Experts

I've a 10Gb UNIX text file and I need to remove the very last line.
Can anyone help me out?
Question by:stummj
  • 2
  • 2
LVL 85

Expert Comment

ID: 16613067
perl -MTie::File -e 'tie @array, 'Tie::File', shift or die $!; pop @array; untie @array' file

Author Comment

ID: 16613089
Cool - but whats the syntax I need to use? which bits do I need to replace with my filename and do I need a redirect to a new file or will it just strip off the last line in the existing file?
LVL 43

Accepted Solution

ravenpl earned 2000 total points
ID: 16613158
head -n -1 filename > new.filename # would also do with less memory consumption, but will create new file.


Author Comment

ID: 16613250
Great! That will do nicely thanks.
LVL 43

Expert Comment

ID: 16613262
But do try ozo's sollution - it's inplace sollution, hence may be better.
perl -MTie::File -e 'tie @array, 'Tie::File', shift or die $!; pop @array; untie @array' FILENAME_HERE

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question