Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1463  / 1 Year ago, sat, january 14, 2023, 5:52:21

This question (How to get values after an equal sign) is basically what I want:


Given a line of text, with named parameter looking syntax like color=red, using bash how can you grab that color value (yielding red)?


Except in that question, the line of text is entirely those key-value pairs, so the accepted answer utilizes bash's eval since that syntax happens fit very nicely.




I'm wondering for a generic line of text like ⎜ ↳ Synaptics TM3289-021 id=13 ] how I could grab that id value (yielding 13)?


Maybe utilizing regex, where I know the line will contain [whitespace]id=<TEXT I WANT>[whitespace]?


More From » bash

 Answers
3

Use grep's -o (output only the matched text) option. Read man grep and do:


echo "bunch of. text id=13 more=4" | 
grep -E -o 'id=[0-9]+`

If you want to be extra careful, match the leading and trailing spaces as well:


echo "bunch of. text id=13 more=4" | 
grep -E -o ' id=[0-9]+ '

[#999] Monday, January 16, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eballxeye

Total Points: 370
Total Questions: 91
Total Answers: 139

Location: Suriname
Member since Sat, Jan 1, 2022
2 Years ago
eballxeye questions
Sun, Jan 8, 23, 18:23, 1 Year ago
Sun, Sep 11, 22, 23:24, 2 Years ago
Tue, Dec 14, 21, 07:15, 2 Years ago
;