“ git stripspace”实际上是如何工作的(无用的文档)

时间:2018-10-12 13:40:47

标签: git removing-whitespace

我正在尝试运行git stripspace,但是documentation却毫无价值。

似乎{em> git stripspacesstdin作为输入并写入stdout,但是:

% git stripspace < myfile.txt > myfile.txt

删除了myfile.txt

中的所有文本

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

2 个答案:

答案 0 :(得分:2)

您要将重定向到同一文件。

这就是这里的问题。

尝试更改您的简单命令,即:

git stripspace < myfile.txt > myfile.txt

对此:

git stripspace < myfile.txt > my-other-file.txt

它应该可以正常工作。

答案 1 :(得分:1)

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%