I have an output like this
I need to get the id 65a8fa6 as an variable to a new command.
I’m familiar with grep and use it to get the line I need. But how do I only pick the first 7 caracters.
This is where I’m now
vagrant global-status | grep DB1
Output
65a8fa6 default vmware_desktop running /Users/USER/Documents/Vagrant/centos7SN/DB1
3
Answers
1st solution: You could simply do this with
awk
. Simply look for stringDB
in line and then if its found then print 1st field of that line. Save the output into variablevar
and later you could use it for your use.OR for matching either
db1
ORDB1
try in anyawk
:2nd solution: If you have GNU
awk
and you want to use ignorecase then try:3rd solution: To get first 7 characters try:
Sed alternative:
Split the output of vagrant … into two sections using sed and regular expressions (-r) Substitute the line for the first section only and print.
You can also use the
cut
command to find instances of what you’re after, provided that there’s some consistent text near what you want to find:Say you want to find
Hello
out of the following line:You can find it doing something like:
Should output
Hello