Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cut or awk command to print first field of first row

Tags:

linux

bash

unix

awk

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 
like image 267
user3331975 Avatar asked Mar 05 '14 07:03

user3331975


1 Answers

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}' 
like image 125
devnull Avatar answered Sep 23 '22 11:09

devnull