Cut 或 awk 命令打印第一行的第一个字段

我正在尝试打印输出第一行的第一个字段。案子是这样的。我只需要从这个输出打印 SUSE

# cat /etc/*release


SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2

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

SUSE
VERSION
PATCHLEVEL
263666 次浏览

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

Specify the Line Number using NR built-in variable.

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

You could use the head instead of cat:

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

Try

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

where NUM is line number

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

try this:

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

if tab delimited:

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

awk, sed, pipe, that's heavy

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

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.

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.

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=_'
awk 'NR==1&&NF=1' file
grep -om1 '^[^ ]\+' file


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