庆典:获取输出的第n列最短的路 [英] bash: shortest way to get n-th column of output

查看:104
本文介绍了庆典:获取输出的第n列最短的路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你的工作日期间,您多次遇到栏目化输出以下形式的一些命令在bash(在我的情况下,从执行 SVN ST 在我的Rails的工作目录)

Let's say that during your workday you repeatedly encounter the following form of columnized output from some command in bash (in my case from executing svn st in my Rails working directory):

?       changes.patch
M       app/models/superman.rb
A       app/models/superwoman.rb

为了与命令的输出工作 - 在这种情况下,文件名 - 某种解析是必需的,使得第二列可以被用作输入下一个命令

in order to work with the output of your command - in this case the filenames - some sort of parsing is required so that the second column can be used as input for the next command.

我一直在做的就是使用 AWK 来获得在第二列,例如当我想删除所有文件(不,这是一个典型的用例:),我会做:

What I've been doing is to use awk to get at the second column, e.g. when I want to remove all files (not that that's a typical usecase :), I would do:

svn st | awk '{print $2}' | xargs rm

由于我这种类型很多,一个自然的问题是:有没有在bash实现这一较短(因而冷却器)的方式

Since I type this a lot, a natural question is: is there a shorter (thus cooler) way of accomplishing this in bash?

请注意:
我所要求本质上是即使我具体的例子是在我的svn工作流程shell命令的问题。如果你觉得工作流程是愚蠢的,并提出另一种方法,我可能不会投你失望,但其他人可能的,因为在这里真正的问题是如何获得在bash的第n列命令的输出,在尽可能短的方式。感谢:)

NOTE: What I am asking is essentially a shell command question even though my concrete example is on my svn workflow. If you feel that workflow is silly and suggest an alternative approach, I probably won't vote you down, but others might, since the question here is really how to get the n-th column command output in bash, in the shortest manner possible. Thanks :)

推荐答案

您可以使用 削减 访问第二场:

You can use cut to access the second field:

cut -f2

编辑:
对不起,没有意识到SVN并不在它的输出使用制表符,所以这是一个有点用处。您可以定制剪切来的输出,但它是一个有点脆弱的 - 像削减-c 10 - 将工作,但精确值将取决于您的安装。

Sorry, didn't realise that SVN doesn't use tabs in its output, so that's a bit useless. You can tailor cut to the output but it's a bit fragile - something like cut -c 10- would work, but the exact value will depend on your setup.

另一个选择是一样的东西: SED'S / \\ s \\ + //

Another option is something like: sed 's/.\s\+//'

这篇关于庆典:获取输出的第n列最短的路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆