[Semibug] Need an example of libxo usage

admin at bclug.ca admin at bclug.ca
Sat Apr 13 11:48:41 EDT 2024


Mike Wayne wrote on 2024-04-12 15:55:

> Not too helpful.
> 
>     -f specifies the lastlogin FILE to use, not fields

Ugh, confidently wrong.


>     As mentioned, I'm trying to use --libxo text, not xml

That's my dumb mistake.


> Looking to get lastlogin to print a single line:
>     wayne last login: Fri Apr 12 15:06:16 2024 from 192.168.63.146
> 
> 
> I'm guessing I need the printf equivalent for libxo but it appears
> not to be documented anywhere.
I think JSON should do it via `jq`.


printf "%s last login %s from %s\n" $(lastlogin --libxo json | jq -r 
'.user,.login-time,.from')


I don't have any BSD system to try this on, but something like that 
ought to work.


Example:

└─» $ printf "%s %s %s\n" $(echo -n '{"user": "Wayne", "from": 
"192.168.63.146", "login_time": "today"}' | jq -r '.user, .login_time, 
.from')
Wayne today 192.168.63.146


A couple things to watch for: JSON / jq won't like a key with a hyphen, 
such as login-time.  I'd assume / hope that libxo would do the 
transformation required for proper JSON.


Also, IFS might need tweaking as the date containing spaces will get 
parsed into separate printf string placeholders.

Or some `sed` transforms.


rb




More information about the Semibug mailing list