Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Best way to remove last line-feed in text file

What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
(as echo -n will remove all trailing newline characters)

MacBook /, Mac OS X (10.6.3), / Windows xp (Boot Camp)

Posted on May 1, 2010 2:30 PM

Reply
Question marked as Best reply

Posted on May 1, 2010 4:38 PM

sed '$d' < file1 > file2; mv file2 file1

"$" means the last line with sed. "d" is delete.

sed does have the ability to edit in place with a backup using the -i switch, but it's risky, so I stick with the write and move method.
7 replies

May 1, 2010 4:46 PM in response to Tony T1

What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
(as echo -n will remove all trailing newline characters)

According to my experiments, you have removed all line terminators from the file, and replaced those between lines with a space.

That is to say, you have turned a multi-line file into one long line with no line terminator.

If that is what you want, and your files are not very big, then your echo statement might be all you need.

If you need to deal with larger files, you could try using the 'tr' command, and something like

tr ' ' ' ' <file.txt >newfile.txt

The only problem with this is, it will most likely give you a trailing space, as the last newline is going to be converted to a space. If that is not acceptable, then something else will have to be arranged.

However, if you really want to maintain a multi-line file, but remove just the very last line terminator, that gets a bit more complicated. This might work for you:

perl -ne '
chomp;
print " " if $n++ != 0;
print;
' file.txt >newfile.txt

You can use cat -e to see which lines have newlines, and you should see that the last line does not have a newline, but all the others still do.

I guess if you really did mean to remove all newline characters and replace them with a space, except for the last line, then a modification of the above perl script would do that:

perl -ne '
chomp;
print " " if $n++ != 0;
print;
' file.txt >newfile.txt

Am I even close to understanding what you are asking for?

May 1, 2010 6:51 PM in response to BobHarris

BobHarris wrote:
What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
(as echo -n will remove all trailing newline characters)

According to my experiments, you have removed all line terminators from the file, and replaced those between lines with a space.


No, echo -n $(cat file.txt) will do as you describe, but not if the command substitution is in quotes: "$(cat file.txt)"

googling found me an awk solution:


awk 'NR > 1 { print h } { h = $0 } END { ORS = ""; print h }' inputfile


...but I never got around to learning awk 🙂

(and the reason is that I'm pulling tag data from a FLAC file, editing it, and then re-insering to the FLAC, and I'm ending up with an extra newline. echo-n "$(metaflac --show-tag=TAG)" > temp.txt pulls the data w/o an extra newline, just didn't know if this was the 'best' solution)

Tony

p.s. actually the awk solution and your perl solution is 'best' as it removes only the last newline while echo -n removes all trailing newlines.

May 3, 2010 8:25 PM in response to gearry


file='/path/to/file'
[[ "$(tail -c 1 "${file}" | tr -dc ' ' | wc -c)" -eq 1 ]] &&
printf "" | dd of="${file}" seek=$(($(stat -f "%z" "${file}") - 1)) bs=1 count=1
tail -c 10 "${file}"; echo

While that is extremely convoluted, I did learn something about dd I never realized before. The following is from the dd man page:
... If an initial portion of the output file is
seeked past (see the oseek operand), the output file is trun-
cated at that point.

My hat goes off to you 🙂

Best way to remove last line-feed in text file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.