How to remove trailing whitespaces for multiple fi

2019-01-21 03:09发布

Are there any tools / UNIX single liners which would remove trailing whitespaces for multiple files in-place.

E.g. one that could be used in the conjunction with find.

7条回答
放我归山
2楼-- · 2019-01-21 03:33

For some reason, the sed and perl commands did not work for me. This did :

find ./ -type f | rename 's/ +$//g'

Feels like the most straight forward one to read as well

查看更多
贪生不怕死
3楼-- · 2019-01-21 03:39

How about this:

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

Btw, this is a handy site: http://sed.sourceforge.net/sed1line.txt

查看更多
SAY GOODBYE
4楼-- · 2019-01-21 03:40

Unlike other solutions which all require GNU sed, this one should work on any Unix system implementing POSIX standard commands.

find . -type f -name "*.txt" -exec sh -c 'for i;do sed 's/[[:space:]]*$//' "$i">/tmp/.$$ && mv /tmp/.$$ "$i";done' arg0 {} +

Edit: this slightly modified version preserves the files permissions:

find . -type f -name "*.txt" -exec sh -c 'for i;do sed 's/[[:space:]]*$//' "$i">/tmp/.$$ && cat /tmp/.$$ > "$i";done' arg0 {} +
查看更多
小情绪 Triste *
5楼-- · 2019-01-21 03:41

ex

Try using Ex editor (part of Vim):

$ ex +'bufdo!%s/\s\+$//e' -cxa *.*

Note: For recursion (bash4 & zsh), you can use a new globbing option (**/*.*). Enable by shopt -s globstar.

perl

find . -type f -name "*.java" -exec perl -p -i -e "s/[ \t]$//g" {} \;

as per Spring Framework Code Style.

sed

For using sed, check: How to remove trailing whitespaces with sed?


See also: How to remove trailing whitespace of all files recursively?

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-21 03:51

For those that are not sed gurus (myself included) I have created a small script to use JavaScript regular expressions to replace text in files and does the replacement in place:

http://git.io/pofQnQ

To remove trailing whitespace you can use it as such:

$ node sed.js "/^[\t ]*$/gm" "" file

Enjoy

查看更多
放荡不羁爱自由
7楼-- · 2019-01-21 03:53

I've been using this to fix whitespace:

while IFS= read -r -d '' -u 9
do
    if [[ "$(file -bs --mime-type -- "$REPLY")" = text/* ]]
    then
        sed -i -e 's/[ \t]\+\(\r\?\)$/\1/;$a\' -- "$REPLY"
    else
        echo "Skipping $REPLY" >&2
    fi
done 9< <(find . \( -type d -regex '^.*/\.\(git\|svn\|hg\)$' -prune -false \) -o -type f -print0)

Features:

  • Keeps carriage returns (unlike [:space:]), so it works fine on Windows/DOS-style files.
  • Only worries about "normal" whitespace - If you have vertical tabs or such in your files it's probably intentional (test code or raw data).
  • Skips the .git and .svn VCS directories.
  • Only modifies files which file thinks is a text file.
  • Reports all paths which were skipped.
  • Works with any filename.
查看更多
登录 后发表回答