Benches macOS

7

Best way to remove last line-feed in text file

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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)

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    LittleSaint

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  2. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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?

  3. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗

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

    That actually deletes the last line, and not just the trailing linefeed.

    But then again, I'm unclear as to exactly what is desired, so your solution could be right.

  4. 0

    Tony T1

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    sed won't work:

    sed '$d'


    Line 1
    Last Line

    becomes


    Line 1

    not


    Line 1
    Last Line

    echo -n works, just not sure if its the best solution

  5. 0

    Tony T1

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  6. 0

    gearry

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    How about using dd to get rid of the file's last newline character?


    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

  7. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗


    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 🙂