简体   繁体   English

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

[英]How do I get a part of the output of a command in Linux Bash?

How do I get a part of the output of a command in Bash?如何在 Bash 中获取命令输出的一部分?

For example, the command php -v outputs:例如,命令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.我只想输出PHP 5.3.28 (cli)部分。 How do I do that?我怎么做?

I've tried php -v | grep 'PHP 5.3.28'我试过php -v | grep 'PHP 5.3.28' 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)这不是我想要的。

You could try the below AWK command,你可以试试下面的AWK命令,

$ php -v | awk 'NR==1{print $1,$2,$3}'
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. NR==1 (condition) 即,仅当 NR 变量的值为 1 时才执行{}的语句。
  • {print $1,$2,$3} Print col1,col2,col3. {print $1,$2,$3}打印 col1,col2,col3。 , in the print statement means OFS (output field separator). ,在打印语句中表示 OFS(输出字段分隔符)。

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

Output:输出:

PHP 5.3.28 (cli)

Or using space as the delimiter:或者使用空格作为分隔符:

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

If you want all the lines that contain "php", do this:如果您想要包含“php”的所有行,请执行以下操作:

 $ php -v | grep -i "php"

Then if 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 $1,$2,$3}'

a classic "million ways to skin a cat" question...一个经典的“给猫剥皮的百万种方法”问题……

These methods seem to filter by spaces... If the versions/notes contain spaces, this fails.这些方法似乎按空格过滤...如果版本/注释包含空格,则失败。

The ( brackets, however, seem consistent across all my platforms so I've used the following:然而, (括号在我的所有平台上似乎都是一致的,所以我使用了以下内容:

For example, on Debian :例如,在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 , Red Hat Linux , or Scientific Linux distribution: ...或在CentOSRed Hat LinuxScientific Linux发行版上:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM