1

I have a file that has this line in it:

$n22(s:Tstring) =   "252";

I'm trying to write a shell command to grep the file and only return 252. This is what I have so far:

grep -o '$n22(s:Tstring).*;' /etc/test/testfile.cfg
$n22(s:Tstring) =   "252";

As you can see, it finds the line, but it returns everything. I just want 252. Can you tell me where I'm going wrong pls?

Thanks

Happydevdays
  • 135
  • 1
  • 3

1 Answers1

2

Does it have to be grep? You could do it with awk like:

awk -F\" '/\$n22\(s:Tstring\)/ {print $2}'

with GNU grep at least you can use perl style regex and do it like so:

 grep -Po '\$n22\(s:Tstring\)[^"]+"\K[^"]+'

the \K means to not include everything that matched up to that point as part of the match for the -o option.

Eric Renouf
  • 939
  • 8
  • 19