Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch specific string using regex

I have multiple boards. Inside my bash script, I want to catch my root filesystem name using regex. When I do a cat /proc/cmdline, I have this:

BOOT_IMAGE=/vmlinuz-5.15.0-57-generic root=/dev/mapper/vgubuntu-root ro quiet splash vt.handoff=7

I just want to select /dev/mapper/vgubuntu-root

So far I have managed to catch root=/dev/mapper/vgubuntu-root using this command

\broot=[^ ]+
like image 914
void_brain Avatar asked Aug 25 '26 14:08

void_brain


1 Answers

You can use your regex in sed with a capture group:

sed -E 's~.* root=([^ ]+).*~\1~' /proc/cmdline

/dev/mapper/vgubuntu-root

Another option is to use awk(should work in any awk):

awk 'match($0, /root=[^ ]+/) {
   print substr($0, RSTART+5, RLENGTH-5)
}' /proc/cmdline

# if your string is always 2nd field then a simpler one
awk '{sub(/^[^=]+=/, "", $2); print $2}' /proc/cmdline
like image 200
anubhava Avatar answered Aug 27 '26 03:08

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!