gpt4 book ai didi

linux - 为什么 awk 没有按预期工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:52 24 4
gpt4 key购买 nike

我正在尝试用制表符分隔一个以冒号分隔的文件。只要我不添加标题,它就可以工作。

这个有效:

[root@cloudops11l ~]# awk 'BEGIN {FS=":";OFS="\t"} {print $1,$3,$4}' /etc/passwd | head -3
root 0 0
bin 1 1
daemon 2 2

这只会弄乱输出中的第一条记录:

[root@cloudops11l ~]# awk 'BEGIN {print "Username\tUID\tGID"} {FS=":";OFS="\t"} {print $1,$3,$4}' /etc/passwd | head -3
Username UID GID
root:x:0:0:root:/root:/bin/bash
bin 1 1

最佳答案

您不再在 BEGIN block 中设置 FSOFS,因此设置仅在处理第一行输入后生效。将其移回 BEGIN block 。

awk 'BEGIN {print "Username\tUID\tGID"; FS=":";OFS="\t"}
{print $1,$3,$4}' /etc/passwd | head -n 3

关于linux - 为什么 awk 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com