2

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.

1
  • 1
    cut -f1 -d' ' maybe. Probably something else is better. Commented Apr 19, 2021 at 6:05

4 Answers 4

3

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.
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps something like vosxlx | sed -n 's/\(vm.*\ \)\(\/.*\)/\1/p'?

Or maybe vosxlx | awk '/vm/ {print $1}'

1 Comment

Thanks, @taylor-g's worked out fine and the simplest but I've been meaning to learn about sed and awk for a while and I keep forgetting. I guess it's as good a time as any. Thanks again!
1
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$

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.