How can I trim the contents of a text file?
12,437
Assuming you want to remove all spaces and newlines, not just the last newline, you can use tr
:
tr -d '[[:space:]]' < file > file.trimmed
Or, more precisely:
tr -d '\t\n ' < file > file.trimmed
Related videos on Youtube
Author by
Lawrence
Updated on September 18, 2022Comments
-
Lawrence over 1 year
I want to remove all "blank" characters from the very beginning and the very end of a text file, including \n if exists. (basically mimicking the behaviour of trim() function of most programming languages, if the "file" was a big string).
-
Admin about 6 yearsCan you provide a sample of the contents of the file?
-
Admin about 6 yearsEach line, or just the very beginning and end?
-
Admin about 6 yearsJust on the definition tack, I believe POSIX requires a newline at the end of a file in order to be called a “text” file. Just FYI.
-
Admin about 6 yearsJeff: the very beginning and end. (main description updated)
-