Problem with Bash output redirection

前端 未结 11 1917
梦毁少年i
梦毁少年i 2020-12-06 06:19

I was trying to remove all the lines of a file except the last line but the following command did not work, although file.txt is not empty.

$cat file.txt |ta         


        
11条回答
  •  萌比男神i
    2020-12-06 07:06

    You can use sed to delete all lines but the last from a file:

    sed -i '$!d' file
    
    • -i tells sed to replace the file in place; otherwise, the result would write to STDOUT.
    • $ is the address that matches the last line of the file.
    • d is the delete command. In this case, it is negated by !, so all lines not matching the address will be deleted.

提交回复
热议问题