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.
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.
You want
sed --in-place 's/[[:space:]]\+$//' file
That will delete all POSIX standard defined whitespace characters, including vertical tab and form feed. Also, it will only do a replacement if the trailing whitespace actually exists, unlike the other answers that use the zero or more matcher (*
).
--in-place
is simply the long form of -i
. I prefer to use the long form in scripts because it tends to be more illustrative of what the flag actually does.
It can be easily integrated with find
like so:
find . -type f -name '*.txt' -exec sed --in-place 's/[[:space:]]\+$//' {} \+
As pointed out in the comments, the above doesn't work if you don't have gnu tools installed. If that's the case, you can use the following:
find . -iname '*.txt' -type f -exec sed -i '' 's/[[:space:]]\{1,\}$//' {} \+
find -exec command
. The first ends with ;
. It runs command
once for every file find
returns. The second ends with +
. It runs command
as few times as possible by building up a list of files to run command
on. Since the ;
variant requires a backslash to escape the ;
, I also generally put it on the +
as well (though I don't think it's strictly necessary for the +
). –
Rehearse -exec
with find
because all that {}+
stuff is like line noise. I prefer find . -type f -name '*.txt' | xargs --replace=FILE sed --in-place 's/foo/baz/' FILE
but YMMV :) –
Quits sed
does not support long options. I was able to get this recipe working by installing GNU sed with Homebrew (brew install gnu-sed
). –
Youngyoungblood sed
will work with the following tweaks find . -type f -name '*.rb' -exec sed -i '' 's/[[:space:]]*$//' {} \+
. Note the -i ''
and that we've replaced the +
with *
. –
Reynaud 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 {} +
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:
[:space:]
), so it works fine on Windows/DOS-style files.file
thinks is a text file.sed -i -e 's/[ \t]\+\(\r\?\)$/\1/'
(same sed wo. adding newline at EOF) isn't preserving DOS-style endings. Using gnu sed 4.8. Example seq 2 | unix2dos | sed -e 's/[ \t]\+\(\r\?\)$/\1/' | xxd -p
outputs 310a320a
should be 310d0a320d0a
–
Baelbeer -b
option to sed
to preserve CLRF
. The regex preserved CLRF
but sed
didn't https://mcmap.net/q/102970/-preserve-line-endings –
Baelbeer How about this:
sed -e -i 's/[ \t]*$//'
Btw, this is a handy site: http://sed.sourceforge.net/sed1line.txt
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?
"s/[ \t]+$//g"
in perl. –
Topical 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:
To remove trailing whitespace you can use it as such:
$ node sed.js "/^[\t ]*$/gm" "" file
Enjoy
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.
© 2022 - 2024 — McMap. All rights reserved.