I have a file with a load of errors in it. I used grep to pull out all the JSONExcpetion Errors and save them in a new file like this:

sudo grep -Hrn "JSONException" 2016-* >> jsonexception.txt

Now I have a file with a lot of JSONExceptions in it. Some of those exceptions contain IDs. I'd like to pull out the ID's and save them to another file.

In my Java code, I use the following regex to identify the correct ID formatting in a String:


Can I use something similar with grep to pull IDs from jsonexception.txt and save them to another file?

A sample from the log:

2016-02/server-2016-02-11-3.log:17425:11/Feb/2016 12:08:35,519 [ERROR] [Thread-3] - WorkerRunnable: Worker 2: A JSONException occurred, on the string: {"DEVICE":[{" ID":"32584305d2ff32051d06dfff"},{"TimeStamp":1455185304,"Event":"Boot","BuildDate":"Feb 11 2016","BuildTime":"10:48:00","Reason":"Power on Reset","FSHigh":485,"FSLow":475 ,"VerMajor":0,"VerMinor":9,"VerRevision":2,"VerPatch":2,"MAC":"00:06:66:3a:26:5c","WlanVersion":"4.75","WlanModuleType":"RN131","BoardRev":"RevD1","DisinfectSeconds":0},{ "TimeStamp":1455185316,"Event":"CurrentSettings","IssuedTime":0,"FSHigh":535,"FSLow":425,"Duty":100,"Frequency":1000,"Current":80,"IdlePeriod":30,"StillOnPeriod":20,"Auto OnPeriod":43200,"AutoOnDuration":10,"MaxOnTime":14400,"FlowSensitivity":50,"LidSwitches":1,"UvFluxWarn":70,"UvFluxInsufficient":40,"Firm Server":"���^A.^B�^B.^P�^K�^G�^C�JF^M� �H�O�zp^HI����^C�^G�^C�^M�B�^N�^R��� ����O^B�pG�^D","FirmPort:1,"DeviceTypeName":"","WlanTimeZone":25,"LedRegArray":[{"Bo ardId":"A","LedNo":0,"RegState":"Regulate"},{"BoardId":"A","LedNo":1,"RegState":"Regulate"}]},{"TimeStamp":1455185316,"Event":"CurrentWlanSettings","SettingsArray":[{"WlanSettingsId":1},{"WlanSettingsId":2},{"WlanSettingsId":3}]},{"TimeStamp":145518531 5,"Event":"Connect","WlanSettingsId":1,"MAC":"00:06:66:3a:26:5c","IP":"","WLAN":"TEST1","RSSI":-52,"Version":"4.75","AP-MAC":"ac:9e:17:ea:0f:3c"},{"End ":100}]}^M

  • Can you show a line from the file you want to grep? – choroba May 13 '16 at 13:51
  • I've added a line above, the ID I want to grep is bolded, it's a 24 char hexadecimal string. – mal May 13 '16 at 14:02

If your grep supports -o, you can use the following:

grep -o '"ID":"[a-f0-9]\{24\}"' | cut -d\" -f4

The first part extracts only the "ID":"..." part, the second one selects the 4th column, using " as the separator, i.e. the id itself.

It's much safer to include the "ID": in the query, otherwise you can accidentally match a different part of the line.

  • Your solution didn't work for me when I used the OP's sample text. However grep -Eo "(ID\":\"[0-9a-zA-Z]*\")" did work for me. Might add it to yours as a possible added solution in-case the first fails. – Yokai Jan 12 '18 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.