Issue
i have file in which i want to remove white space from the end of line test.txt is as following (there is white space at the end of line.
ath-miRf10005-akr
ath-miRf10018-akr
ath-miRf10019-akr
ath-miRf10020-akr
ath-miRf10023-akr
ath-miRf10024-akr
i used sed 's/$\s//'
but it is not working.
Solution
Use either a simple blank *
or [:blank:]*
to remove all possible spaces at the end of the line:
sed 's/ *$//' file
Using the [:blank:]
class you are removing spaces and tabs:
sed 's/[[:blank:]]*$//' file
Note this is POSIX, hence compatible in both GNU sed and BSD.
For just GNU sed you can use the GNU extension \s*
to match spaces and tabs, as described in BaBL86's answer. See POSIX specifications on Basic Regular Expressions.
Let's test it with a simple file consisting on just lines, two with just spaces and the last one also with tabs:
$ cat -vet file
hello $
bye $
ha^I $ # there is a tab here
Remove just spaces:
$ sed 's/ *$//' file | cat -vet -
hello$
bye$
ha^I$ # tab is still here!
Remove spaces and tabs:
$ sed 's/[[:blank:]]*$//' file | cat -vet -
hello$
bye$
ha$ # tab was removed!
Answered By - fedorqui 'SO stop harming' Answer Checked By - Candace Johnson (WPSolving Volunteer)