A
A
achirkov02021-02-02 08:15:16
ubuntu
achirkov0, 2021-02-02 08:15:16

grep - how to make an exact match?

There is data from qm list:

100 srv1             running    16384            100.00 965
       101 srv2             running    12314            100.00 900
       1000 srv3              running    2048            100.00 85
       1001 srv4              running    2048           100.00 35
       10000 srv4              running    2048           100.00 480

Purpose: to get the third column by the number in the first. Current variant:
sudo qm list | grep 100 | awk '{print $3}'
If I do grep 100 - all will be displayed. But you only need to get one value.
Tried grep -F 100 - no change.
I tried grep -w 100 - the values ​​100 and 101 are displayed.
How to get only one line?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lynn "Coffee Man", 2021-02-02
@achirkov0

Why is grep here? awk is very good at comparing, and, unlike grep, it can work with individual fields, and not just with the whole line.
sudo qm list | awk '{ if ($1 == 100) print $3}'

P
planc, 2021-02-02
@planc

you can use:
^ - start of line
$ - end of line
grep ^100$

S
Saboteur, 2021-02-02
@saboteur_kiev

grep just finds 100 lines elsewhere.
Write a regular expression that will look for the first 100, such as the beginning of a string, possibly spaces, and 100:

sudo qm list | grep  -P '^[ ]*100' | awk '{print $3}'

or 100 not followed by a dot
sudo qm list | grep  -P '100[^.]' | awk '{print $3}'

D
divian, 2021-07-26
@divian

And the simplest thing is to put a space in quotation marks after what you are looking for ))
| grep "100"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question