50

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
aksg24
  • 661
  • 1
  • 5
  • 7
  • 1
    I think the command should be `sed 's/\s*$//'` instead. The `$` designates and end-of-line anchor so nothing comes after that on each line. –  Dec 11 '13 at 14:35
  • right $ define end of object (working buffer) so there is nothing after the end. For same reason there is nothing befoire the begining (^) so any pattern have ^as first element if present and $ at end if present (for POSIX compliance, not true when some action like | [logical OR] is used) – NeronLeVelu Dec 11 '13 at 15:00

6 Answers6

76

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!
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    Just curious: will this work for `\t` and similar whitespace characters? What about using `[:space:]`? –  Dec 11 '13 at 14:36
  • 1
    Good question, @HermanTorjussen. To do that I noticed you have to use `[[:space:]]`. – fedorqui Dec 11 '13 at 14:38
  • 4
    the character class `[:space:]` comprises all whitespace (including newlines and carriage returns) -- `[:blank:]` is only horizontal whitespace (space and tab) -- see https://en.wikipedia.org/wiki/Regular_expression#Character_classes – glenn jackman Dec 11 '13 at 15:34
  • Overall should I do: `sed 's/[[:blank:]]*$//' file > file` to update file? @fedorqui – alper Sep 25 '18 at 11:08
  • 1
    @alper nop, `whatever file > file` will inevitably truncate `file`. You can either use `sed -i.bak '...' file` to perform an in-place edit or `sed '...' file > new_file`. – fedorqui Sep 27 '18 at 08:57
  • As Glenn said, and depending on (a) your OS (*nix, Mac, Win*) the line endings are different combination of `[\n\r]`, and (b) the file encoding (*ASCII, UTF-8, UTF-16*, etc), the success of `sed` with `[:blank:]` is not at all guaranteed. However, by ensuring your file and terminal are using the same character set encoding, you can convert line endings to what you need, while removing the white space, using **`[:space:]`**. – not2qubit Nov 25 '20 at 02:14
18

Try this:

sed -i 's/\s*$//' youfile.txt

On OS X:

sed -i '' 's/\s*$//' youfile.txt
Zach Tuttle
  • 2,165
  • 1
  • 14
  • 17
BaBL86
  • 2,602
  • 1
  • 14
  • 13
2
sed -i 's/[[:blank:]]\{1,\}$//' YourFile

[:blank:] is for space, tab mainly and {1,} to exclude 'no space at the end' of the substitution process (no big significant impact if line are short and file are small)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
2

If your lines are exactly the way you depict them(no leading or embedded spaces), the following should serve as well

awk '{$1=$1;print}' file.txt
iruvar
  • 22,736
  • 7
  • 53
  • 82
0

This might work for you (GNU sed):

sed -ri  '/\s+$/s///' file

This looks for whitespace at the end of the line and and if present removes it.

potong
  • 55,640
  • 6
  • 51
  • 83
-1

Try using

cat kb.txt | sed -e 's/\s$//g'
chopper
  • 6,649
  • 7
  • 36
  • 53
Kamal
  • 1