How do I get a part of the output of a command in Linux BASH?
As the title says, how do I get a part of the output of a command in Bash?
For example, the command php -v
outputs:
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?
I've tried 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.
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
(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) In pure bash you can do
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
if you want all the lines that contain "php" do this:
$ php -v | grep -i "php"
then supposedly you want the first three words within those you can add another pipe as @Avinash suggested:
$ php -v | grep -i "php" | awk 'NR==1{print $1,$2,$3}'
链接地址: http://www.djcxy.com/p/9816.html
上一篇: 我可以通过索引内容覆盖工作副本吗?