Home > Enterprise >  How to copy some arguments from a command output(bash)?
How to copy some arguments from a command output(bash)?

Time:09-30

I need to copy some arguments from my command. For example:

last -1x shutdown output will be shutdown system down 5.10.17_1 Mon Jan 1 01:00 - 12:00 (11:00)

How do I copy only the 11:00 to bash variable?

CodePudding user response:

try this:

last -1x shutdown | sed -n '1s/.*(\(..:..\))/\1/p'

CodePudding user response:

Considering the tags you used I guess you are limited to bash only. This should work in your specific case which consists in selecting the (only) part between parentheses:

v=$(last -1x shutdown)
v="${v#*\(}"
v="${v%*\)*}"
echo "$v"
11:00

Or, with recent enough bash versions (at least bash 3.0, if I remember well):

[[ $(last -1x shutdown) =~ .*\((.*)\) ]]
v="${BASH_REMATCH[1]}"
echo "$v"
11:00
  • Related