cut or awk command to print first field of first row

感情迁移 提交于 2020-03-17 03:48:51

问题


I am trying print the first field of the first row of an output. Here is the case. I just need to print only SUSE from this output.

# cat /etc/*release

SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2

Tried with cat /etc/*release | awk {'print $1}' but that print the first string of every row

SUSE
VERSION
PATCHLEVEL

回答1:


Specify NR if you want to capture output from selected rows:

awk 'NR==1{print $1}' /etc/*release

An alternative (ugly) way of achieving the same would be:

awk '{print $1; exit}'

An efficient way of getting the first string from a specific line, say line 42, in the output would be:

awk 'NR==42{print $1; exit}'



回答2:


Specify the Line Number using NR built-in variable.

awk 'NR==1{print $1}' /etc/*release



回答3:


try this:

head -1 /etc/*release | awk '{print $1}'



回答4:


You could use the head instead of cat:

head -n1 /etc/*release | awk '{print $1}'



回答5:


sed -n 1p /etc/*release |cut -d " " -f1

if tab delimited:

sed -n 1p /etc/*release |cut -f1



回答6:


awk, sed, pipe, that's heavy

set `cat /etc/*release`; echo $1



回答7:


Try

sed 'NUMq;d'  /etc/*release | awk {'print $1}'

where NUM is line number

ex. sed '1q;d'  /etc/*release | awk {'print $1}'



回答8:


You can kill the process which is running the container.

With this command you can list the processes related with the docker container:

ps -aux | grep $(docker ps -a | grep container-name | awk '{print $1}')

Now you have the process ids to kill with kill or kill -9.



来源:https://stackoverflow.com/questions/22190902/cut-or-awk-command-to-print-first-field-of-first-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!