首页 > 解决方案 > 'git stripspace' 如何实际工作(文档无用)

问题描述

我正在尝试运行git stripspace,但文档毫无价值。

似乎其作为输入git stripspacesstdin写入stdout但:

% git stripspace < myfile.txt > myfile.txt

删除了里面的所有文字myfile.txt

我希望文档提供了一个调用示例。有人有吗?

标签: gitremoving-whitespace

解决方案


sponge是解决这个问题的工具。它首先“吸收”所有通过管道传输的数据,然后将其写入指定的文件。它是moreutils软件包的一部分 - 在 Ubuntu 上安装它sudo apt install moreutils

这是一个使用文件的示例spacetest.txt- 该awk命令用于帮助可视化文件中存在的尾随空格:

# print the file spacetest.txt with '%' appended to each line-end to show trailing spaces

~/temp/stripspace-test$ awk '{ print $0 "%" }' spacetest.txt 
%
%
line 1 has a space at the end %
line 2 has 2 spaces at the end  %
line 3 has 3 spaces at the end   %
%
the next line has 4 spaces%
    %
three blank lines follow this one%
%
%
%

# 'redirect' from and to spacetest.txt using `sponge` as a go-between

~/temp/stripspace-test$ git stripspace < spacetest.txt | sponge  spacetest.txt


# print the file spacetest.txt again to demonstrate everything is as expected

~/temp/stripspace-test$ awk '{ print $0 "%" }' spacetest.txt 
line 1 has a space at the end%
line 2 has 2 spaces at the end%
line 3 has 3 spaces at the end%
%
the next line has 4 spaces%
%
three blank lines follow this one%

推荐阅读