简体   繁体   English

在While循环中创建新空格的Bash Awk

[英]Bash Awk in While Loop Creating Blank Spaces

I have been using shell scripting for a few months now, so I am still a beginner and I need your kind assistance in the below issue: 我已经使用Shell脚本几个月了,所以我仍然是一个初学者,在以下问题上我需要您的帮助:

I have a floating license report that shows the list of users, their IDs, how many users are using the license, how many minutes the license has been used and the status of the license. 我有一个浮动的许可证报告,其中显示了用户列表,其ID,使用许可证的用户数量,已使用许可证的分钟数以及许可证的状态。 I need to print from it the list of licneses that have been used for 15 minutes or more and from that list print a list of how many users are using each license, but the condition is to get the licenses with the status"r". 我需要从中打印使用了15分钟或更长时间的许可证列表,并从该列表中打印使用每个许可证的用户数列表,但条件是获取状态为“ r”的许可证。 The second list I am assigning it to a variable as I will be using it later on in the code. 我将第二个列表分配给变量,因为稍后将在代码中使用它。

this is the code I wrote: 这是我写的代码:

cat $license_report | awk '{if ($3 >=15) print $1}' | while read line 

license_report is where I stored the report. license_report是我存储报告的地方。 $3 is the column for how many minutes the license has been used, $1 is the column for the users IDs, $2 is the column for how many users are using the license and $4 is for the status of the license. $ 3是使用许可证的分钟数的列,$ 1是用户ID的列,$ 2是使用许可证的用户数的列,$ 4是许可证的状态。 my approach here is to use a while loop to list the licenses that have been used for 15 minutes and then cat the report again, grep the result I got from the loop and use awk to get the licenses with "r" status 我在这里的方法是使用while循环列出已使用15分钟的许可证,然后再次提交报告,grep我从循环中获得的结果,并使用awk获得状态为“ r”的许可证

do
running_licenses=$(cat $license_report | grep -w $line| awk '{if ($4 != "r") print $3}')

this code runs fine without any errors, but the list I get has empty spaces. 这段代码运行正常,没有任何错误,但是我得到的列表有空白。 so at the beginning it shows me four lines and the rest is empty. 所以在开始时它显示了四行,其余为空。 this is the output I am getting 这是我得到的输出

3
5
6
2
#empty space
#empty space
#empty space
#empty space
#empty space
#empty space
#empty space

I think I am getting the empty spaces because in the list I got from the while loop, not all of licenses have status "r", so it's printing the licenses with the status "r" and leaving the rest as a blank space. 我想我得到的是空格,因为在从while循环获得的列表中,并非所有许可证的状态均为“ r”,因此它将状态为“ r”的许可证打印出来,其余的保留为空白。

My question is, how can I just print the licenses with the status "r" without printing the empty spaces? 我的问题是,如何只打印状态为“ r”的许可证而不打印空白? Also, do you know a better approach to achieve what I am trying to do? 另外,您知道一种更好的方法来实现我的目标吗?

Thanks a lot for your help in advance. 非常感谢您的提前帮助。

为什么只使用awk:

running_licenses=$(awk '$3>=15 && $4 != "r"{print $3}' $license_report)

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

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