gpt4 book ai didi

linux - Finger 命令仅显示用户名

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:17 26 4
gpt4 key购买 nike

我是新来的,希望了解有关 bash 编程的更多信息。

首先我需要一些有关手指命令的帮助。当我只使用“手指”时,这就是我得到的输出,显然带有一些数据集。

Login    Name   Tty   Idle   Login Time   Where

我想要的是调整 Finger 命令,使其仅输出“名称”及其关联的数据集,如下所示:

Name
...

最佳答案

您可以使用 awk 来实现:

finger | awk '{print $2}'

编辑:使用 awkcut 组合的新方法,对于任意格式的名称来说更加稳健。

#!/bin/bash
#parse_finger.sh

#Read first line from stdin
IFS='$\n' read -r line

#Count the number of chars until 'Name'
str=$(echo "$line" | awk -F "Name" '{print $1}')
start=${#str}
start=$((start+1))

#Count the number of chars until 'Tty'
str=$(echo "$line" | awk -F "Tty" '{print $1}')
stop=${#str}
stop=$((stop-1))

#Print out the 'Name' header
echo "$line" | cut -c $start-$stop

#Read in the rest of our lines and print the cols we care about
while IFS='$\n' read -r line; do
echo "$line" | cut -c $start-$stop
done

手指运行它 | parse_finger.sh

关于linux - Finger 命令仅显示用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48270710/

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