如何';git stripspace';实际工作(文档无用)

如何';git stripspace';实际工作(文档无用),git,removing-whitespace,Git,Removing Whitespace,我试图运行git-stripspace,但它出人意料地一文不值 似乎git stripspaces将stdin作为其输入并写入stdout,但是: % git stripspace < myfile.txt > myfile.txt %git stripspacemyfile.txt 删除了myfile.txt中的所有文本 我希望文档提供一个调用示例。有人有吗?您正在将从和重定向到同一个文件 这就是问题所在 尝试更改您的简单命令,如下所示: git stripspace <

我试图运行
git-stripspace
,但它出人意料地一文不值

似乎
git stripspaces
stdin
作为其输入并写入
stdout
,但是:

% git stripspace < myfile.txt > myfile.txt
%git stripspacemyfile.txt
删除了
myfile.txt中的所有文本


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

您正在将重定向到同一个文件

这就是问题所在

尝试更改您的简单命令,如下所示:

git stripspace < myfile.txt > myfile.txt
git stripspacemyfile.txt
为此:

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

它应该可以很好地工作。

海绵
就是解决这个问题的工具。它首先“吸收”所有通过管道输入的数据,然后将其写入指定的文件。它是
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%
#打印文件spacetest.txt,并在每行末尾追加“%”,以显示尾随空格
~/temp/stripspace test$awk'{print$0“%”}spacetest.txt
%
%
第1行末尾有一个空格%
第2行末尾有2个空格%
第3行末尾有3个空格%
%
下一行有4个空格%
%
这一行后面有三个空行%
%
%
%
#使用“海绵”作为中间人从和重定向到spacetest.txt
~/temp/stripspace test$git stripspace
命令的问题不在于stripspace的文档记录不完整,而是因为您正在将输出重定向到正在读取的同一个文件。这不适用于任何命令。尝试重定向到其他文件。
git stripspace myfile.txt是否有效?不是git做的。将标准输出重定向到文件时,它首先创建该文件。在您的情况下,它创建为空(换句话说,它重写了您的文件)。啊哈。谢谢我没有意识到这一点。我同意文档可以稍微清晰一些,也可以说它对清理后的文本做了什么,还可以提供一个实际的执行示例,但我不一定同意它毫无价值。