Sed command read last line and remove carriage return

To read the last line of a file using sed command and remove the carriage return, you can use the following command:

sed -e '$!d' -e 's/\r$//' filename.txt

Here's what each part of the command does:

  • -e '$!d': Deletes all lines except the last line. $ symbol refers to the last line and !d means delete all lines except the last one.
  • -e 's/\r$//': Removes the carriage return from the last line. \r represents the carriage return character, and $ means it should be at the end of the line. s means substitution, and the empty string between the second pair of slashes means it should be replaced with nothing.
  • filename.txt: The name of the file you want to operate on.

This command will output the last line of the file filename.txt without a carriage return.

 

أحدث أقدم