Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep: invalid repetition count

I'm very experienced with regex, but cannot figure out why this isn't working.

My sample text:

{
    "coord":
    {
        "lon":-74.01,
        "lat":40.71
    },
    "sys":
    {
        "message":0.2452,
        "country":"United States of America",
        "sunrise":1394191161,
        "sunset":1394232864
    },
    "weather":
    [
        {
            "id":803,
            "main":"Clouds",
            "description":"broken clouds",
            "icon":"04n"
        }
    ],
    "base":"cmc stations",
    "main":
    {
        "temp":270.54,
        "pressure":1035,
        "humidity":53,
        "temp_min":270.15,
        "temp_max":271.15},
        "wind":
        {
            "speed":2.1,
            "deg":130},
            "clouds":
            {
                "all":75
            },
            "dt":1394149980,
            "id":5128581,
            "name":"New York",
            "cod":200
        }
    }
}

I'm trying to grab weather[0].id.

My full script (the curl gets the JSON):

curl -s "http://api.openweathermap.org/data/2.5/weather?q=NYC,NY" 2>/dev/null | grep -e '"weather":.*?\[.*?\{.*?"id": ?\d{1,3}'

I always get the error

grep: invalid repetition count(s)
like image 447
jdotjdot Avatar asked Mar 07 '14 01:03

jdotjdot


1 Answers

grep -e does not recognize \d as digits. It does not recognize non-greedy forms like .*?. For the grep portion of your command, try:

grep -e '"weather":[^[]*\[[^{]*{[^}]*"id": *[0-9]\{1,3\}'

Alternatively, it your grep supports it (GNU), use the -P option for perl-like regex and your original regex will work:

grep -P '"weather":.*?\[.*?\{.*?"id": ?\d{1,3}'
like image 71
John1024 Avatar answered Oct 26 '22 03:10

John1024