剪切或awk命令打印第一行的第一个字段。

时间:2022-02-03 08:57:45

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.

我正在尝试打印输出的第一行的第一个字段。这是如此。我只需要打印输出的SUSE。

# 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

尝试使用cat /etc/ release函数| awk {'print $1} *,但是它打印每一行的第一个字符串

SUSE
VERSION
PATCHLEVEL

7 个解决方案

#1


93  

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

如果您想从选定的行捕获输出,请指定NR:

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:

从某一行(例如输出中的第42行)获取第一个字符串的有效方法是:

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

#2


10  

Specify the Line Number using NR built-in variable.

使用NR内置变量指定行号。

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

#3


6  

try this:

试试这个:

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

#4


0  

You could use the head instead of cat:

你可以用头代替猫:

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

#5


0  

Try

试一试

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

where NUM is line number

哪里是NUM的行号?

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

#6


0  

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

if tab delimited:

如果以制表符定界的:

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

#7


0  

awk, sed, pipe, that's heavy

awk, sed,管道,很重

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

#1


93  

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

如果您想从选定的行捕获输出,请指定NR:

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:

从某一行(例如输出中的第42行)获取第一个字符串的有效方法是:

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

#2


10  

Specify the Line Number using NR built-in variable.

使用NR内置变量指定行号。

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

#3


6  

try this:

试试这个:

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

#4


0  

You could use the head instead of cat:

你可以用头代替猫:

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

#5


0  

Try

试一试

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

where NUM is line number

哪里是NUM的行号?

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

#6


0  

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

if tab delimited:

如果以制表符定界的:

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

#7


0  

awk, sed, pipe, that's heavy

awk, sed,管道,很重

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