I have some files that should not have any linefeed (newline) characters in them, but they've been corrupted with the addition of a few lines of text at the very beginning of the file that are terminated with a line ending character. For example:
Code:
This is a line of text\n
This is another line\n
And another\n
original-file-data-no-terminator...
where \n above is x'0a', the newline or linefeed character. The rest of the file following the third line in the example above is also text data, but it doesn't (and shouldn't) have any line ending characters.
For this example, wc -l will tell me I have four lines, even though the last one does not have a line ending character.
I can delete the garbage lines at the beginning like this:
Code:
$ wc -l file
4 file
$ sed '1,3d' file
How could I combine these into one command so that the output from the wc command can be used in the sed command to delete all the lines before the last one? It seems like this should be pretty easy, but I can't figure it out.
Another way to think of this is that I want to retain only the last "line" of the file. I want to delete all lines from the file except for the last line. Does that suggest another solution?
Thanks!