Reputation: 123
I have this crazy long command-turned-(bash)script that outputs a tiny table with and object and some other data relate to it. I want to single the name of the object out to use it as a variable to further run more commands.
It outputs something like this:
ID PATH NAME VERSION UPGRADE STATUS
vm-13034 /abc/def somethingelse vX.X.X-XXX-XXXXXXX Up to date
The value I'm interested in is ID, which always is vm-#####. With an online tool I came up with the regex ^vm-\d{5,}$
(,
bc the number is incrementing). So I figured vosxls | grep -E ^vm-\d{5,}$
(vosxls is the script's name for the long command) would work but it returns nothing. I tried it with -w
, -e
, grep -e "vm-\d{5,}" <(vosxls)
and a few more adding and remove the enclosing ^
& $
characters when it would throw an error, enclosing the string in soft- and hard quotes or nothing at all, whatever would work.
Everything else that didn't error, returned nothing.
I read some examples with Perl and stuff similar to that but I stayed away and generally have stayed as basic as possible so I can run it in another system without issues. The most I've diverted from the most basic stuff (which are all I know anyway) was egrep
which is I believe is the same as grep -e
if I'm not mistaken.
How can the string be printed out? Is it because I'm already using a script it somehow affects how grep works? The script has no variables, it is merely a shorthand to avoid typing a lot of difficult to remember syntax+values.
Thanks.
Upvotes: 2
Views: 162
Reputation: 22225
You can't use \d
with the -E
option of grep to denote a digit. -P
, respectively -Po
, will work. But if you already know that you need the first field of the last line of the output, you don't need grep. A
vosxlx | tail -n 1 | cut -d ' ' -f 1
would do as well.
Upvotes: 0
Reputation: 711
Could be as simple as
vosxls | grep -Eo "^vm-\d{5,}"
Specifying -o allows you to return whatever matches the regex only.
-o, --only-matching
Prints only the matching part of the lines.
Upvotes: 3
Reputation: 3880
foo | perl -ne 'print $1 if /^(vm-\d+)/'
Your version of grep might not have extended regex support:
$:/mnt/c/Users/sukuj$ cat /tmp/s.txt
ID PATH NAME VERSION UPGRADE STATUS
vm-13034 /abc/def somethingelse vX.X.X-XXX-XXXXXXX Up to date
vm-23034 /abc/def somethingelse vX.X.X-XXX-XXXXXXX Up to date
$:/mnt/c/Users/sukuj$ grep -oE 'vm-[0-9]+' /tmp/s.txt
vm-13034
vm-23034
$:/mnt/c/Users/sukuj$ grep -oE 'vm-[0-9]{5,}' /tmp/s.txt
vm-13034
vm-23034
$:/mnt/c/Users/sukuj$ grep -V
grep (GNU grep) 3.4
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
suku@DESKTOP-GID8MQV:/mnt/c/Users/sukuj$
Upvotes: 1
Reputation: 26505
Perhaps something like vosxlx | sed -n 's/\(vm.*\ \)\(\/.*\)/\1/p'
?
Or maybe vosxlx | awk '/vm/ {print $1}'
Upvotes: 1