为什么'git stripspace'会删除所有文本?

fruv7luv  于 2022-12-28  发布在  Git
关注(0)|答案(2)|浏览(76)

我正在尝试运行git stripspace,但documentation没有帮助。
git stripspaces似乎将stdin作为其输入并写入stdout,但:

% git stripspace < myfile.txt > myfile.txt

删除了myfile.txt中的所有文本
我希望文档提供一个调用示例。有人有吗?

anauzrmj

anauzrmj1#

您正在将和**重定向到同一个文件。
这就是问题所在。
尝试更改您的简单命令,即:

git stripspace < myfile.txt > myfile.txt

改为:

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

它应该能正常工作。

arknldoa

arknldoa2#

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%

相关问题