bash awk 命令输出前的 echo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15188177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:44:02  来源:igfitidea点击:

Echo before the output of the awk command

bashshellawkterminalsh

提问by Sec AnonOps

I need to know how to add this line before each output of the awkcommands. I also need to add how much disk space has been used by the user in their home directory. I have no idea how to combine them guys.

我需要知道如何在awk命令的每个输出之前添加这一行。我还需要添加用户在其主目录中使用了多少磁盘空间。我不知道如何将他们结合起来。

This is how I list users with their home directory.

这就是我列出用户及其主目录的方式。

awk -F":" '{print }' /etc/passwd | sort | grep /home

The output is:

输出是:

/home/testuser
/home/anonsec

My desired output is:

我想要的输出是:

Home Directory: /home/testuser
Disk Space Used (KB): 23342 KB

Home Directory: /home/anonsec
Disk Space Used (KB): 23342 KB

采纳答案by user1685841

This works for me:

这对我有用:

for i in `awk -F":" '{print }' /etc/passwd | sort | grep /home`;  do
        echo "Home Directory:" $i
        echo "Disk Space Used (KB):" `du -s $i |cut -f1`
        echo ""
done

You might want or need to improve it a bit if you have some things in /etc/passwdthat you are not interested in.

如果你有一些/etc/passwd你不感兴趣的东西,你可能想要或需要稍微改进一下。

回答by Chris Seymour

$ du -s /home/* | awk '{print "Home Directory:",,"\nDisk Space Used","\n"}'

Home Directory: /home/luser 
Disk Space Used 42222768