简体   繁体   English

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

[英]cut or awk command to print first field of first row

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}'尝试使用cat /etc/*release | awk {'print $1}' cat /etc/*release | awk {'print $1}' but that print the first string of every row cat /etc/*release | awk {'print $1}'但打印每一行的第一个字符串

SUSE
VERSION
PATCHLEVEL

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

使用NR内置变量指定行号

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

试试这个:

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

您可以使用head代替cat

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

如果制表符分隔:

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

Try尝试

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

where NUM is line number其中 NUM 是行号

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

awk、sed、管道,这很重

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:使用此命令,您可以列出与 docker 容器相关的进程:

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 .现在您有了要使用killkill -9杀死的进程 ID。

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.或者使用 while 循环,但这可能是一种不好的方式。

the most code-golfy way i could think of to print first line only in awk :我能想到的最代码高尔夫方式仅在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.第一次通过退出块, "_"未定义,因此它失败并跳过第 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).那么同一个计数器的递减将使它在awk的眼中为“”(任何不是空字符串或数字零的东西在它们敏捷的布尔意义上都被认为是“真”)。 that same counter also triggers default action of print for row 1.相同的计数器还会触发第 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最后,在第 2 行的开头,它符合进入操作块的条件,指示它立即退出,从而执行与

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.对于单行打印,甚至不值得将FS设置为跳过字段分割部分。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM