Caltech Library logo

How to trim a trailing newline from a text file.

Given the following text in a file name t.txt where the last line contains a trailing newline.

    one
    two
    three

Running split to create an JSON array yields an extra empty string.

    string -i t.txt split '\n'

Yields

    ["one","two","three",""]

To avoid the trailing empty string in the array you can trimspace first then do your split on newlines.

    string -i t.txt trimspace | split -i - '\n'

Yields

    ["one","two","three"]