111

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
0

10 Answers 10

259

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}'
7
  • @jaypal I also thought of adding a tac file | awk 'END{print $1}' but then realized it might be a bit too much.
    – devnull
    Commented Mar 5, 2014 at 7:15
  • Though that would make your ugly solution look remarkably prettier against the proposed hideous solution! Commented Mar 5, 2014 at 7:19
  • 4
    It not ugly. Its even better, since on a large file, it will save lots of time by exit on found and stops processing.
    – Jotne
    Commented Mar 5, 2014 at 8:02
  • @Jotne I termed it ugly simply because NR==1 was implicit. Added another example in the answer that probably clarifies a bit more.
    – devnull
    Commented Mar 5, 2014 at 8:07
  • @devnull I do know, but OPs requirement is clear print the first string of the first row of an output so here the exit would be just fine.
    – Jotne
    Commented Mar 5, 2014 at 8:12
25

Specify the Line Number using NR built-in variable.

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

try this:

head -1 /etc/*release | awk '{print $1}'
6
df -h | head -4 | tail -1 | awk '{ print $2 }'

Change the numbers to tweak it to your liking.

Or use a while loop but thats probably a bad way to do it.

1
  • Now that's a nifty hack! Commented Aug 3, 2021 at 8:06
4

You could use the head instead of cat:

head -n1 /etc/*release | awk '{print $1}'
3
sed -n 1p /etc/*release |cut -d " " -f1

if tab delimited:

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

awk, sed, pipe, that's heavy

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

Try

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

where NUM is line number

ex. sed '1q;d'  /etc/*release | awk {'print $1}'
2
  • This would print entire 1st line and not 1st field as OP stated. Commented Mar 5, 2014 at 7:24
  • Answer saras che pan thodu laambu che. Khali sed thi pan kari sakiye - sed -r '1s/([^ ]+) .*/\1/;q' /etc/*release :) Commented Mar 5, 2014 at 7:37
0

the most code-golfy way i could think of to print first line only in awk :

awk '_{exit}--_'    # skip the quotations and make it just
                    #   awk _{exit}--_
                    #
                    # if u're feeling adventurous 
  1. first pass through exit block, "_" is undefined, so it fails and skips over for row 1.

  2. then the decrementing of the same counter will make it "TRUE" in awk's eyes (anything not empty string or numeric zero is considered "true" in their agile boolean sense). that same counter also triggers default action of print for row 1.

    —- incrementing… decrementing… it's same thing, 
       merely direction and sign inverted.
    
  3. then finally, at start of row 2, it hits criteria to enter the action block, which instructs it to instantly exit, thus performing essentially the same functionality as

awk '{ print; exit }'

… in a slightly less verbose manner. For a single line print, it's not even worth it to set FS to skip the field splitting part.

using that concept to print just 1st row 1st field :

awk '_{exit} NF=++_'
awk '_++{exit} NF=_'
0
awk 'NR==1&&NF=1' file
grep -om1 '^[^ ]\+' file

# multiple files
awk 'FNR==1&&NF=1' file1 file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.