Skip to content
Advertisement

Using sed piped with w command show user with the largest idle time

the w command produces something like this:

JavaScript

I must get a sed script that prints the user with the longest idle time. But the problem is that w shows the idle time in 3 different formats: 1) in seconds: ending with an ‘s’, 2) mm:ss (not ending with ‘m’ or ‘s’), and 3) hh:mm (ending with ‘m’). So I need to somehow make a sed script that can work with that and I cannot seem to do it.

My attempt: have 2 variables that will first be set just to find out if the highest idle time is in form 1 or 2 or 3. That part is done. Then based on that I was gonna extract the text in that idle field and compare it with a variable and replace if longer time. But it has a letter at the end! so it can’t be compared as a number. It is a string. This is the problem.

JavaScript

Advertisement

Answer

The idle time is nothing more complicated than the last access time of the TTY device that the user is logged in from. So a simple way of doing this would be to grab all the names of the TTYs people are logged in on and stat them:

JavaScript

On some systems, that will emit errors for login sessions such as X11 sessions which aren’t on real ttys.

If you want the age instead of the absolute time, post-process it to subtract it from the current time:

JavaScript

Or use perl‘s -A operator:

JavaScript

From here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement