如何在Linux BASH中获得命令输出的一部分? [英] How do I get a part of the output of a command in Linux BASH?

查看:254
本文介绍了如何在Linux BASH中获得命令输出的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,命令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.

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

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

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

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.

推荐答案

您可以尝试以下awk命令,

You could try the below awk command,

$ 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(条件),即,仅当NR变量的值为1时,才执行{}中的语句.
  • {print $1,$2,$3}打印col1,col2,col3.打印语句中的,表示OFS(输出字段分隔符)
  • 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)

这篇关于如何在Linux BASH中获得命令输出的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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