I have the following output of the command "xm list":
Name ID Mem VCPUs State Time(s)
Domain-0 0 505 4 r----- 11967.2
test1 28 1024 1 -b---- 137.9
test2 33 1024 1 -b---- 3.2
I execute a shellscript with: ./myscript test2 In this script, I need the ID of test2 (shown at the command "xm list" (ID33)) I tried it with grep and cut like this:
xm list | grep $1 | cut ???
How does this work?
What about using awk?
xm list | awk '/^test2/ {print $2}'
I added ^
in /^test/
so that it checks this text in the beginning of the line. Also awk '$1=="test2" {print $2}'
would make it.
$ cat a
Name ID Mem VCPUs State Time(s)
Domain-0 0 505 4 r----- 11967.2
test1 28 1024 1 -b---- 137.9
test2 33 1024 1 -b---- 3.2
$ awk '/^test2/ {print $2}' a
33
With cut
you cannot treat multiple consecutive delimiters as one, so cut -d ' '
will not work.
Either use awk
as in the other answer, or use tr
to "squeeze" the spaces before using cut
:
xm list | grep test2 | tr -s ' ' | cut -d ' ' -f2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With