Jay Taylor's notes
back to listing indexHow can I delete a newline if it is the last character in a file?
[web search]
I have some files that I'd like to delete the last newline if it is the last character in a file. 'od -c' shows me that the command I run does write the file with a trailing new line:
I've tried a few tricks with sed but the best I could think of isn't doing the trick:
Any ideas how to do this? |
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
or, to edit the file in place:
[Editor's note: This was described as a 'perl blasphemy' on the awk website I saw. But, in a test, it worked. |
|||||||||||||||||||||||||||||||||
|
You can take advantage of the fact that shell command substitutions remove trailing newline characters: Simple form that works in bash, ksh, zsh:
Portable (POSIX-compliant) alternative (slightly less efficient):
Note:
A guide to the other answers:
|
|||||||||
|
You can do this with
To test for an ending newline you can use
You could also use
|
|||||||||
|
|
|||||||||||||||||||||||||||||||||
|
gawk
|
|||||||||||||||
|
If you want to do it right, you need something like this:
We open the file for reading and appending; opening for appending means that we are already This runs in constant time and constant space for any input, and doesn't require any more disk space, either. |
|||||||||||||||
|
Here is a nice, tidy Python solution. I made no attempt to be terse here. This modifies the file in-place, rather than making a copy of the file and stripping the newline from the last line of the copy. If the file is large, this will be much faster than the Perl solution that was chosen as the best answer. It truncates a file by two bytes if the last two bytes are CR/LF, or by one byte if the last byte is LF. It does not attempt to modify the file if the last byte(s) are not (CR)LF. It handles errors. Tested in Python 2.6. Put this in a file called "striplast" and
P.S. In the spirit of "Perl golf", here's my shortest Python solution. It slurps the whole file from standard input into memory, strips all newlines off the end, and writes the result to standard output. Not as terse as the Perl; you just can't beat Perl for little tricky fast stuff like this. Remove the "\n" from the call to Put this into "slurp_and_chomp.py" and then run
|
|||||||||
|
Yet another perl WTDI:
|
|||
$ perl -e 'local $/; $_ = <>; s/\n$//; print' a-text-file.txt |
|||||||||||||||||||||||||||||||||
|
Using dd:
|
|||
A very simple method for single-line files, requiring GNU echo from coreutils:
|
|||||||||
|
Assuming Unix file type and you only want the last newline this works.
It will not work on multiple newlines... * Works only if the last line is a blank line. |
|||||||||
|
Yet another answer FTR (and my favourite!): echo/cat the thing you want to strip and capture the output through backticks. The final newline will be stripped. For example:
|
|||||||||
|
|
|||||||||
|
The only time I've wanted to do this is for code golf, and then I've just copied my code out of the file and pasted it into an |
|||||||||||||||
|
I had a similar problem, but was working with a windows file and need to keep those CRLF -- my solution on linux:
|
|||
|
|||||||||
|
ruby:
or:
|
||||
POSIX SED:
|
|||
Should remove any last occurence of \n in file. Not working on huge file (due to sed buffer limitation) |
|||
Your Answer
Not the answer you're looking for? Browse other questions tagged linux perl shell awk sed or ask your own question.
asked |
7 years ago |
viewed |
56557 times |
active |
Get the weekly newsletter! In it, you'll get:
- The week's top questions and answers
- Important community announcements
- Questions that need answers
see an example newsletter
Linked
Related
Hot Network Questions
- Generate antsy permutations
- Why do dealers in Vegas check ID even if I look older than 25?
- A weird and spooky clock
- What is the max time it would take yeast to "clean up"?
- Are there textual deviations between the Dead Sea Scrolls and the Old Testament?
- How much and what type of damage does Warlock Thought Shield deal?
- Why does Wolfram Alpha say the roots of a cubic involve square roots of negative numbers, when all three roots are real?
- How do I find more resources?
- deer in German: Hirsch, Reh
- How do I disable or lock lookup fields on a Visual Force page?
- Is the Set designed properly?
- Does the reciprocal of a probability represent anything?
- What is Wilson's theorem?
- How to prepare for impending doom
- How common is it to use the word 'bitch' for a female dog?
- my matrix doesnt fit the page
- Maximum server memory
- What are the alternatives to compound interest for a Muslim?
- English fellow vs Arabic fellah
- "/usr/bin/ping" is shown as yellow-on-red in the default Fedora bash color scheme -- what does it mean?
- Why does the kill-screen glitch occur in Pac-man?
- Is there any way to bring an egg to its natural state (not boiled) after you cook it?
- Why was Vader surprised that Obi-Wan's body disappeared?
- Securing a LAN that has multiple exposed external at Cat 6 cable runs?
Technology | Life / Arts | Culture / Recreation | Science | Other | ||||||
---|---|---|---|---|---|---|---|---|---|---|