如何在 Linux BASH 中获取命令输出的一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25116521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:01:24  来源:igfitidea点击:

How do I get a part of the output of a command in Linux BASH?

linuxbash

提问by William Edwards

As the title says, how do I get a part of the output of a command in Bash?

正如标题所说,如何在 Bash 中获取命令输出的一部分?

For example, the command php -voutputs:

例如,命令php -v输出:

PHP 5.3.28 (cli) (built: Jun 23 2014 16:25:09) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies with the ionCube PHP Loader v4.6.1, Copyright (c) 2002-2014, by ionCube Ltd.

and I only want to output the PHP 5.3.28 (cli)part, how do I do that?

我只想输出PHP 5.3.28 (cli)部分,我该怎么做?

I've tried php -v | grep 'PHP 5.3.28'but that outputs: PHP 5.3.28 (cli) (built: Jun 23 2014 16:25:09)and that's not what I want.

我试过,php -v | grep 'PHP 5.3.28'但输出:PHP 5.3.28 (cli) (built: Jun 23 2014 16:25:09)这不是我想要的。

回答by Avinash Raj

You could try the below awk command,

你可以试试下面的 awk 命令,

$ php -v | awk 'NR==1{print ,,}'
PHP 5.3.28 (cli)

It prints the first three columns from the first line of input.

它从输入的第一行打印前三列。

  • NR==1(condition)ie, execute the statements within {}only if the value of NR variable is 1.
  • {print $1,$2,$3}Print col1,col2,col3. ,in the print statement means OFS(Output Field Seperator)
  • NR==1(条件)即,{}仅当 NR 变量的值为 1 时才执行其中的语句。
  • {print $1,$2,$3}打印 col1,col2,col3。,在打印语句中表示 OFS(输出字段分隔符)

回答by slavik

In pure bash you can do

在纯粹的 bash 中你可以做

echo 'PHP 5.3.28 (cli) (built: Jun 23 2014 16:25:09)' | cut -d '(' -f 1,2

Out:

出去:

PHP 5.3.28 (cli)

Or using space as delimiter

或者使用空格作为分隔符

echo 'PHP 5.3.28 (cli) (built: Jun 23 2014 16:25:09)' | cut -d ' ' -f 1,2,3

回答by nafas

if you want all the lines that contain "php" do this:

如果您想要包含“php”的所有行,请执行以下操作:

 $ php -v | grep -i "php"

then supposedly you want the first three words within those you can add another pipe as @Avinash suggested:

那么据说您想要其中的前三个词,您可以按照@Avinash 的建议添加另一个管道:

$ php -v | grep -i "php" | awk 'NR==1{print ,,}'

回答by starkers

a classic "million ways to skin a cat" question..

一个经典的“给猫剥皮的百万种方法”问题。

These methods seem to filter by spaces.. if the versions/notes contains spaces this fails.

这些方法似乎按空格过滤……如果版本/注释包含空格,则失败。

The (brackets however seem consistent across all my platforms so I've used the following:

(然而,括号在我的所有平台上似乎都是一致的,所以我使用了以下内容:

EG: on debian:

EG:在 debian 上:

root@host:~# php -v  | head -1 
PHP 5.3.28-1~dotdeb.0 with Suhosin-Patch (cli) (built: Dec 13 2013 01:38:56) 
root@host:~# php -v  | head -1 | cut -d " " -f 1-2
PHP 5.3.28-1~dotdeb.0

So here I trim everything before the second (:

所以在这里我在第二个之前修剪所有东西(

root@host:~# php -v  | head -1 | cut -d "(" -f 1-2
PHP 5.3.28-1~dotdeb.0 with Suhosin-Patch (cli) 

note: there will be a trailing white-space. (blank space at the end)

注意:会有一个尾随空格。(末尾有空格)

Alternatively you could always use your package manager to determine this (recommended):

或者,您可以始终使用您的包管理器来确定这一点(推荐):

root@debian-or-ubuntu-host:~# dpkg -s php5 | grep 'Version'
Version: 5.3.28-1~dotdeb.0

....or on a centos/redhat/scientic linux distro:

....或在 centos/redhat/scientic linux 发行版上:

[root@rpm-based-host ~]# rpm -qa | grep php-5
php-5.4.28-1.el6.remi.x86_64