How do i delete any sequence of trailing spaces & tabs at the end of the line in sed?



  • Man page for sed: https://www.lightnetics.com/post/3334

    The sample file is:

    cat -A nonprintchars 
    This is my tab^I$
    This is my space $
    This is my$
    ^M$
    $
    $
    $
    

    Use this, $ signs just denote the end of the line, they are not in the output normally, we only did tabs and spaces not newline character that's why you still see ^M

    $ sed 's/[ ][^ ]*$//' nonprintchars|cat -A
    This is my$
    This is my space$
    This is$
    ^M$
    $
    $
    $
    

    To delete the ^M as well do this:

    sed -e 's/[ ][^ ]*$//' -e '/^M/d' nonprintchars|cat -A
    


© Lightnetics 2024