140

I have a simple shell script that removes trailing whitespace from a file. Is there any way to make this script more compact (without creating a temporary file)?

sed 's/[ \t]*$//' $1 > $1__.tmp
cat $1__.tmp > $1
rm $1__.tmp
kenorb
  • 155,785
  • 88
  • 678
  • 743
Viktor
  • 6,739
  • 7
  • 26
  • 29
  • 2
    You can use `mv` instead of `cat` and `rm`. Why are you using `cat` like that anyway? Why not use `cp`? – Dennis Williamson Dec 14 '10 at 11:14
  • 1
    I used the knowledge I learned from this question to create [a shell script for recursively removing trailing whitespace](https://gist.github.com/dtuite/6314519). – David Tuite Aug 23 '13 at 01:06
  • 1
    Your solution is actually better when using MinGW due to a bug in sed on Windows: http://stackoverflow.com/questions/14313318/permission-denied-when-sed-in-place-edit-in-mingw – Cody Piersall Apr 17 '14 at 18:04
  • See also: [How to remove trailing whitespaces for multiple files?](http://stackoverflow.com/q/10711051/562769) – Martin Thoma Oct 14 '15 at 12:36
  • Note that using `cat` to overwrite the original file rather than `mv` will actually replace the data in the original file (ie, it will not break hard links). Using `sed -i` as proposed in many solutions will not do that. IOW, just keep doing what you're doing. – William Pursell Aug 22 '19 at 17:26

12 Answers12

206

You can use the in place option -i of sed for Linux and Unix:

sed -i 's/[ \t]*$//' "$1"

Be aware the expression will delete trailing t's on OSX (you can use gsed to avoid this problem). It may delete them on BSD too.

If you don't have gsed, here is the correct (but hard-to-read) sed syntax on OSX:

sed -i '' -E 's/[ '$'\t'']+$//' "$1"

Three single-quoted strings ultimately become concatenated into a single argument/expression. There is no concatenation operator in bash, you just place strings one after the other with no space in between.

The $'\t' resolves as a literal tab-character in bash (using ANSI-C quoting), so the tab is correctly concatenated into the expression.

Tom McClure
  • 6,699
  • 1
  • 21
  • 21
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 1
    I get the following on my machine which I cannot update: `sed: Not a recognized flag: i` – javaPlease42 May 19 '14 at 17:14
  • This will also muck with line endings on Windows for some reason. Use caution. – void.pointer May 23 '14 at 15:26
  • 2
    hm. its also buggy in the sense that it will remove all trailing "t"s :) – Good Person Aug 01 '14 at 02:30
  • 2
    "sed: Not a recognized flag: i –" This happens on OSX. You need to add an extension for the backup file after -i on Macs. e.g.: sed -i .bak 's/[ \t]*$//' $1 – Aimon Bustardo Aug 08 '14 at 16:11
  • 1
    @GoodPerson If you were't kidding, you likely forget to escape the `t` :) `\t` is a tab, for those who may not already know. – Sean Allred Dec 12 '14 at 19:27
  • 2
    @SeanAllred was not kidding: its utterly broken unless you happen to be using GNU sed (which is broken in so many other ways) – Good Person Dec 14 '14 at 20:21
  • sed -i 's/[ \t]+$//' "$1" to ensure that there is present at least one whitespace... – Martin Dvorak Sep 25 '15 at 14:44
  • The OS X version works for me. And as a bonus, it also adds a newline to the end of files that are missing one at the end! – occulus Mar 26 '18 at 09:43
  • Just so that others know (because there's "too many pending edits on SO" right now and I can't add this to the answer), you'll have to replace `$1` with the filename of your file, in order to use these. – Nike Dec 28 '22 at 21:56
69

At least on Mountain Lion, Viktor's answer will also remove the character 't' when it is at the end of a line. The following fixes that issue:

sed -i '' -e's/[[:space:]]*$//' "$1"
Asclepius
  • 57,944
  • 17
  • 167
  • 143
acrollet
  • 921
  • 7
  • 6
20

Thanks to codaddict for suggesting the -i option.

The following command solves the problem on Snow Leopard

sed -i '' -e's/[ \t]*$//' "$1"
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Viktor
  • 6,739
  • 7
  • 26
  • 29
  • i found this here, http://joemaller.com/823/quick-note-about-seds-edit-in-place-option/ – Viktor Dec 15 '10 at 02:43
  • 8
    Like @acrollet says, you cannot use `\t` with sed other than GNU sed and it gets interpreted as a literal letter `t`. The command only appears to work, probably because there are no TAB's in the trailing whitespace nor a `t` at the end of a sentence in your file. Using `''` without specifying a backup suffix is not recommended. – Scrutinizer Mar 15 '13 at 08:03
  • If the resolution is indicated for Snow Leopard only, maybe the question should be 'how to remove trailing whitespace on Macos????' – Boyd Jun 25 '21 at 16:40
14

It is best to also quote $1:

sed -i.bak 's/[[:blank:]]*$//' "$1"
Scrutinizer
  • 9,608
  • 1
  • 21
  • 22
5
var1="\t\t Test String trimming   "
echo $var1
Var2=$(echo "${var1}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo $Var2
Sandip Patel - SM
  • 3,346
  • 29
  • 27
  • 1
    Hey, that's just what I needed! The other sed solutions posted had issue integrating with a piped (and piped and piped...) variable assignment in my bash script, but yours worked out of the box. – Eric L. Jun 12 '14 at 18:09
5

I have a script in my .bashrc that works under OSX and Linux (bash only !)

function trim_trailing_space() {
  if [[ $# -eq 0 ]]; then
    echo "$FUNCNAME will trim (in place) trailing spaces in the given file (remove unwanted spaces at end of lines)"
    echo "Usage :"
    echo "$FUNCNAME file"
    return
  fi
  local file=$1
  unamestr=$(uname)
  if [[ $unamestr == 'Darwin' ]]; then
    #specific case for Mac OSX
    sed -E -i ''  's/[[:space:]]*$//' $file
  else
    sed -i  's/[[:space:]]*$//' $file
  fi
}

to which I add:

SRC_FILES_EXTENSIONS="js|ts|cpp|c|h|hpp|php|py|sh|cs|sql|json|ini|xml|conf"

function find_source_files() {
  if [[ $# -eq 0 ]]; then
    echo "$FUNCNAME will list sources files (having extensions $SRC_FILES_EXTENSIONS)"
    echo "Usage :"
    echo "$FUNCNAME folder"
    return
  fi
  local folder=$1

  unamestr=$(uname)
  if [[ $unamestr == 'Darwin' ]]; then
    #specific case for Mac OSX
    find -E $folder -iregex '.*\.('$SRC_FILES_EXTENSIONS')'
  else
    #Rhahhh, lovely
    local extensions_escaped=$(echo $SRC_FILES_EXTENSIONS | sed s/\|/\\\\\|/g)
    #echo "extensions_escaped:$extensions_escaped"
    find $folder -iregex '.*\.\('$extensions_escaped'\)$'
  fi
}

function trim_trailing_space_all_source_files() {
  for f in $(find_source_files .); do trim_trailing_space $f;done
}
Pascal T.
  • 3,866
  • 4
  • 33
  • 36
4

For those who look for efficiency (many files to process, or huge files), using the + repetition operator instead of * makes the command more than twice faster.

With GNU sed:

sed -Ei 's/[ \t]+$//' "$1"
sed -i 's/[ \t]\+$//' "$1"   # The same without extended regex

I also quickly benchmarked something else: using [ \t] instead of [[:space:]] also significantly speeds up the process (GNU sed v4.4):

sed -Ei 's/[ \t]+$//' "$1"

real    0m0,335s
user    0m0,133s
sys 0m0,193s

sed -Ei 's/[[:space:]]+$//' "$1"

real    0m0,838s
user    0m0,630s
sys 0m0,207s

sed -Ei 's/[ \t]*$//' "$1"

real    0m0,882s
user    0m0,657s
sys 0m0,227s

sed -Ei 's/[[:space:]]*$//' "$1"

real    0m1,711s
user    0m1,423s
sys 0m0,283s
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
2

In the specific case of sed, the -i option that others have already mentioned is far and away the simplest and sanest one.

In the more general case, sponge, from the moreutils collection, does exactly what you want: it lets you replace a file with the result of processing it, in a way specifically designed to keep the processing step from tripping over itself by overwriting the very file it's working on. To quote the sponge man page:

sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before writing the output file. This allows constructing pipelines that read from and write to the same file.

https://joeyh.name/code/moreutils/

1

Just for fun:

#!/bin/bash

FILE=$1

if [[ -z $FILE ]]; then
   echo "You must pass a filename -- exiting" >&2
   exit 1
fi

if [[ ! -f $FILE ]]; then
   echo "There is not file '$FILE' here -- exiting" >&2
   exit 1
fi

BEFORE=`wc -c "$FILE" | cut --delimiter=' ' --fields=1`

# >>>>>>>>>>
sed -i.bak -e's/[ \t]*$//' "$FILE"
# <<<<<<<<<<

AFTER=`wc -c "$FILE" | cut --delimiter=' ' --fields=1`

if [[ $? != 0 ]]; then
   echo "Some error occurred" >&2
else
   echo "Filtered '$FILE' from $BEFORE characters to $AFTER characters"
fi
David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
1

These answers confused me. Both of these sed commands worked for me on a Java source file:

  • sed 's/\s\+$/ filename
  • sed 's/[[:space:]]\+$// filename

for test purposes, I used:

 $ echo "  abc       " | sed 's/\s\+$/-xx/'
abc-xx
 $ echo -e "  abc   \t\t    " | sed 's/\s\+$/-xx/'
abc-xx

Replacing all trailing whitespace with "-xx".

@Viktor wishes to avoid a temporay file, personally I would only use the -i => in-place with a back-up suffix. At least until I know the command works.

Sorry, I just found the existing responses a little oblique. sed is straightforward tool. It is easier to approach it in a straightforward way 90% of the time. Or perhaps I missed something, happy to corrected there.

will
  • 4,799
  • 8
  • 54
  • 90
1

To remove trailing whitespace for all files in the current directory, I use

ls | xargs sed -i 's/[ \t]*$//'
Thorkil Værge
  • 2,727
  • 5
  • 32
  • 48
-1

To only strip whitespaces (in my case spaces and tabs) from lines with at least one non-whitespace character (this way empty indented lines are not touched):

sed -i -r 's/([^ \t]+)[ \t]+$/\1/' "$file"
phk
  • 2,002
  • 1
  • 29
  • 54