Print a multiple value after the pattern match in a single line

advertisements

I have single line output as follows

"ng-num_sta" : 5 ,
"ng-num_sta" : 10 ,
"ng-num_sta" : 15 ,
"ng-num_sta" : 3 ,
xyz value : 1 , blah blah, single line

I want to get values for string ng-num_sta only

example:

5
10
15
3

I have tried following but its printing pattern matching only,

 cat myfile.txt | grep -Po '(ng-user-num_sta.)[^\s]*'

OUTPUT:

ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"
ng-user-num_sta"


Your regEx match is incorrect, use GNU grep with -P, PCRE mode enabled as and \K to ignore the pattern match up-to that point.

grep -oP '\"ng-num_sta\" : \K[^ ]*' file
5
10
15
3

Check the regEx here.