How to remove last line from a file in unix




















Full Man Repository. Advanced Search. Contact Us. Forum Rules. Mark Forums Read. Thread Tools. View Public Profile for alpesh. Find all posts by alpesh. View Public Profile for vgersh Find all posts by vgersh View Public Profile for alister. Find all posts by alister. View Public Profile for ddreggors. Find all posts by ddreggors. Page 1 of 2. Asked 10 years, 11 months ago. Active 2 months ago. Viewed k times. I have a file, foo. Mateusz Piotrowski 6, 9 9 gold badges 46 46 silver badges 74 74 bronze badges.

Fragsworth Fragsworth Add a comment. Active Oldest Votes. Ken Y-N On Mac OS X as of Also, head -n -1 won't currently work on a Mac. It's question about removing last line, so I think this is the most important part for everyone viewing this question. Thanks : — kravemir.

It is a sed command. When specifying a location called "range" in sed lingo before a command, that command is only applied to the specified location. So, this command explicitly says "in the range of the last line in a file, delete it". Quite slick and straight to the point, if you ask me. How do your remove the last line but only if it's an empty line?

Show 3 more comments. This is by far the fastest and simplest solution, especially on big files: head -n -1 foo. Nakilon John John 3, 1 1 gold badge 11 11 silver badges 2 2 bronze badges. Thumbs up because it worked, but for all the comparison to sed , this command is pretty darn slow as well — Jeff Diederiks. Mateen Ulhaq 20k 14 14 gold badges 79 79 silver badges bronze badges.

Yossi Farjoun Yossi Farjoun 1, 3 3 gold badges 16 16 silver badges 24 24 bronze badges. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Asked 7 years, 4 months ago. Active 7 years, 4 months ago. The following sed command removes all the lines except the header line. You can specify the list of lines you want to remove in sed command with semicolon as a delimiter. However, this sed do not remove the lines that contain spaces. In the following examples, the sed command deletes the lines in file which match the given pattern. Above sed command removes all the lines that start with character 'u'.

The above command deletes all the lines that end with character 'x'. Here the sed command removes the line that matches the pattern fedora and also deletes all the lines to the end of the file which appear next to this matching line.



0コメント

  • 1000 / 1000