bash 在标准输入中使用 linux “cut”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2226762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using linux "cut" with stdin
提问by jdm
I'm trying to pipe data into "cut" to, say, cut away the first column of text. This works
我正在尝试将数据通过管道传输到“剪切”中,例如,剪切掉第一列文本。这有效
$ cat test.txt | cut -d\ -f2-
Reading from stdin also works:
从标准输入读取也有效:
$ cut -d\ -f2- -
? doc/html/analysis.html
? doc/html/classxytree-members.html
<CTRL+D>
However, as soon as a pipe is involved, it doesn't accept my <CTRL+D>anymore, and I can't signal "end of file":
但是,一旦涉及管道,它就不再接受我的<CTRL+D>,并且我无法发出“文件结束”的信号:
$ cut -d\ -f2- - | xargs echo
Update:This is apparently a bug in an old version of bash (3.00.15). It does work in more recent versions (tried 4.0.33 and 3.2.25). It would be nice to have some workaround, though, since I can't easily upgrade.
更新:这显然是旧版 bash (3.00.15) 中的一个错误。它确实适用于更新的版本(尝试过 4.0.33 和 3.2.25)。不过,最好有一些解决方法,因为我无法轻松升级。
Background: I've got a script/oneliner that gives me a condensed output of cvs status(I know, CVS...) in the form
背景:我有一个脚本/oneliner,它cvs status以以下形式为我提供了(我知道,CVS ...)的浓缩输出
? filename
e.g. for a file not committed yet. I'd like to be able to copy+paste parts of the output from that command and use this as an input to another command, that adds these files to cvs. Say:
例如,对于尚未提交的文件。我希望能够从该命令复制+粘贴部分输出,并将其用作另一个命令的输入,将这些文件添加到 cvs。说:
$ cut -d\ -f2- | xargs cvs add
<paste lines>
<CTRL-D> # <-- doesn't work
Ideas?
想法?
采纳答案by Lachlan Roche
have you tried
你有没有尝试过
$ cat | cut -d\ -f2- | xargs cvs add
<paste lines>
<CTRL-D> # <-- doesn't work
回答by Brian Campbell
Your examples work fine for me. What shell are you using? What utilities?
你的例子对我来说很好。你用的是什么壳?什么公用事业?
One thing that sometimes trips people up is that Ctrl-Donly works if it's the first character in the line. If you copy and paste, you might sometimes accidentally have whitespace as the first character of the line, or no newline at the end of the pasted block, in which case Ctrl-Dwon't work. Just hit return and then try Ctrl-Dagain and see if that fixes your problem.
有时会让人绊倒的一件事是Ctrl-D只有当它是行中的第一个字符时才有效。如果您复制和粘贴,有时您可能会意外地将空格作为行的第一个字符,或者在粘贴块的末尾没有换行符,在这种情况下Ctrl-D将不起作用。只需回车,然后尝试Ctrl-D再次,看看是否能解决你的问题。

