1

I'd like to get the quoted string from output of lvscan which is :

  ACTIVE            '/dev/mysys/root' [297.46 GiB] inherit

What I've done is to use code below:

lvscan | grep -o "[^']\+\b'"

and what I got is :

/dev/mysys/root'

if I remove the very last single quote, I got three lines:

ACTIVE
/dev/mysys/root
[297.46 GiB] inherit

I also tried:

lvscan | grep -o "[@(^'\+.*)].*\>'"
lvscan | grep -o "[^']\+\>'"

What did I miss here?

Any comments would appreciate. Thank you.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Tao Wang
  • 186
  • 1
  • 18

2 Answers2

2

You can use cut instead of grep:

lvscan | cut -f2 -d\'
choroba
  • 231,213
  • 25
  • 204
  • 289
1

if you have -P with grep you can use

lvscan | grep -oP "(?<=')[^']+(?=')"

vks
  • 67,027
  • 10
  • 91
  • 124
  • can also use `grep -oP "'\K[^']+(?=')"` – Sundeep Oct 26 '16 at 02:44
  • Obviously, I'm not using Perl, and l'll check it out tomorrow, this. – Tao Wang Oct 26 '16 at 05:49
  • 1
    Thanks a lot, vks, this line worked for me, I don't really care whether it's Perl Regex or not. An interesting question still remained though, is there a Basic Regex solution for this? – Tao Wang Oct 26 '16 at 15:33
  • @TaoWang basic regex can be used like `'([^']*)'` and then grab the group 1 but i have not used that with grep so m not sure – vks Oct 26 '16 at 15:57
  • `lvscan | grep -Go "'.*'"` is the simplest answer I've tried for Basic Regex if ignoring single quotes pair. – Tao Wang Oct 26 '16 at 17:17
  • 1
    The reason I wanna remove quotes is I'd like to print the output to a variable for later use. Insisting on keeping tried is for learning purpose, thanks again. – Tao Wang Oct 26 '16 at 17:25