简体   繁体   中英

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:

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. 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". 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. $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. 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

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.

My question is, how can I just print the licenses with the status "r" without printing the empty spaces? 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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