0

I found this to remove whitespace from the end of a script How to remove trailing whitespaces with sed? but it doesn't quite do what I was hoping. What I would like to do when I think of remove all white space is to remove also any empty lines - I think that this sed just removes spaces and tabs, but can it be expanded to also trim out any empty lines from the end of the file? Maybe it's not possible to do this with one line, and maybe there are better ways to achieve this, any options are great.

Also, am I right in thinking that this should replace the file in place with the changes? I'm just not sure that's happening in my testing.

sed -i 's/[ \t]*$//' ~/.bashrc
# -i is in place, [ \t] applies to any number of spaces and tabs before the end of the file "*$"
oguz ismail
  • 1
  • 16
  • 47
  • 69
YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 2
    See [How to remove multiple newlines at EOF?](https://unix.stackexchange.com/a/81687/279389) at Unix & Linux SE. – Wiktor Stribiżew Feb 25 '21 at 18:04
  • Thanks for the link @Wiktor. Just any file with white space, tabs and newlines at the end of it Cyrus, literally anything, basically, I just want to trim back anything "empty" at the end of a file back to the first occurrence of a non-whitespace character. – YorSubs Feb 25 '21 at 18:14
  • 1
    That's perfect @Wiktor, exactly what I was after(!). Many thanks. – YorSubs Feb 25 '21 at 18:16

2 Answers2

2

To remove all whitespace at the end of the file:

perl -0777 -pe 's{\s+\z}{}m' foo > bar

To change the file in-place:

perl -i.bak -0777 -pe 's{\s+\z}{}m' foo

To replace all whitespace at the end of the file with a single newline:

perl -0777 -pe 's{\s+\z}{\n}m' foo > bar

To change the file in-place:

perl -i.bak -0777 -pe 's{\s+\z}{\n}m' foo

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.
-0777 : Slurp files whole.

\s+\z : one or more whitespace characters (including newline) at the end of the string (which happens to be the entire file).

The regex uses this modifier:
/m : Allow multiline matches.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 1
    Great. I don't tend to use perl much (I tend to stick to grep, awk, sed), but these are all really nice options, thanks. – YorSubs Feb 25 '21 at 19:47
0

This might work for you (GNU sed):

sed ':a;/\S/!{$d;N;ba}' file

Append empty lines to the previous line.

If the empty line is the last, delete the current pattern space.

Otherwise print the pattern space.

To remove spaces from the end of all lines too:

sed ':a;/\S/!{$d;N;ba};s/ *$//mg' file

or:

sed 'H;$!d;x;s/.//;s/ *$//mg;s/\n*$//' file
potong
  • 55,640
  • 6
  • 51
  • 83