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

后端 未结 4 1982
我寻月下人不归
我寻月下人不归 2021-02-06 16:08

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         


        
4条回答
  •  滥情空心
    2021-02-06 16:35

    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 separator).

提交回复
热议问题