Issue
I have a file and want to look for value from that file using shell script.
So I'm trying to grep value using the below code.
VAR=($grep -Po "(?source-no": ")(\d+)" file)
but it's giving an error
grep missing )
Data in the file looks like below
"source-no" : "123456",
Solution
1st solution: Considering that you need to get digits as output, you could try following then. You are on right path, you need to fix 2 things here: correct your matching regex, where in look behind you need to fix "
in it. Then to save it into shell variable: use grep
command inside $(grep command)
in this form.
grep -oP '(?<="source-no" : ")(\d+)' Input_file
To save this into a variable try following:
var=$(grep -oP '(?<="source-no" : ")(\d+)' Input_file)
Also try to keep shell variables name with small letters, which is recommended by experts IMHO.
2nd solution:
grep -oP '(?<= ")[\w\.]+(?=")' Input_file
(?<= ")
match the pattern begin with, as (?=")
match the pattern end with,[\w\d\.]+
match the value you need that consist by word or digital or dot(.) which I guess the value pattern.
3rd solution: OR using both positive lookahead and positive lookbehind here:
grep -oP '(?<="source-no" : ")\d+(?=")' Input_file
Answered By - RavinderSingh13 Answer Checked By - Dawn Plyler (WPSolving Volunteer)