Issue
How do I grep something and prevent the phrase appearing in the results?
I am trying to get only the MAC address from multiple systems and want only the MAC address in the results.
ipmitool lan print | grep "MAC Address : "
# => MAC Address : aa:bb:cc:dd:ee:ff
I got as far using google as trying is by adding -oP
and \K\w+
but for some reason it only get's the first 2 digits of the MAC:
ipmitool lan print | grep -oP "MAC Address : \K\w+"
# => aa
I understand the -o
will only show the matched text which is why the MAC address is not being shown up and -P
used with \K
will prevent everything in-between from showing up but not sure what exactly to put after \w+
to make the rest of the MAC address show up.
Solution
You can match it with
grep -oP 'MAC Address : \K\S+' file
Here,
-o
- extracts the matched texts, not whole linesP
- enables the PCRE regex syntaxMAC Address :
- matches a literal text\K
- match reset operator that discards the text matched so far from the memory match buffer\S+
- one or more non-whitespace chars.
Answered By - Wiktor Stribiżew Answer Checked By - Marie Seifert (WPSolving Admin)