cut or awk command to print first field of first r

2020-05-13 12:18发布

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

8条回答
Rolldiameter
2楼-- · 2020-05-13 12:40

Specify the Line Number using NR built-in variable.

awk 'NR==1{print $1}' /etc/*release
查看更多
闹够了就滚
3楼-- · 2020-05-13 12:45
sed -n 1p /etc/*release |cut -d " " -f1

if tab delimited:

sed -n 1p /etc/*release |cut -f1
查看更多
一纸荒年 Trace。
4楼-- · 2020-05-13 12:48

Try

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

where NUM is line number

ex. sed '1q;d'  /etc/*release | awk {'print $1}'
查看更多
三岁会撩人
5楼-- · 2020-05-13 12:49

try this:

head -1 /etc/*release | awk '{print $1}'
查看更多
迷人小祖宗
6楼-- · 2020-05-13 12:51

awk, sed, pipe, that's heavy

set `cat /etc/*release`; echo $1
查看更多
▲ chillily
7楼-- · 2020-05-13 12:51

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}'
查看更多
登录 后发表回答