cut or awk command to print first field of first row

LinuxBashUnixAwk

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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}'

Solution 2 - Linux

Specify the Line Number using NR built-in variable.

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

Solution 3 - Linux

try this:

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

Solution 4 - Linux

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.

Solution 5 - Linux

You could use the head instead of cat:

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

Solution 6 - Linux

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

if tab delimited:

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

Solution 7 - Linux

Try

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

where NUM is line number

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

Solution 8 - Linux

awk, sed, pipe, that's heavy

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

Solution 9 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3331975View Question on Stackoverflow
Solution 1 - LinuxdevnullView Answer on Stackoverflow
Solution 2 - Linuxjaypal singhView Answer on Stackoverflow
Solution 3 - LinuxdeveloperView Answer on Stackoverflow
Solution 4 - Linuxretr0manView Answer on Stackoverflow
Solution 5 - LinuxJunaid18183View Answer on Stackoverflow
Solution 6 - LinuxRaha PazokiView Answer on Stackoverflow
Solution 7 - LinuxJayesh BhoiView Answer on Stackoverflow
Solution 8 - LinuxEmmanuelView Answer on Stackoverflow
Solution 9 - Linuxuser3322829View Answer on Stackoverflow