linux - Using grep to find lines that each contain ALL search strings -


i have file contains lot of lines similar this:

{"id": 2796, "some_model": "profile", "message_type": "model_save", "fields": {"account": 14, "address": null, "modification_timestamp": "2014-03-19t10:46:33.543z", "was_deleted": false}} 

but want find lines contain pieces of respective lines want only. example applied in example line above be:

~$ grep '2796' file.log | grep 'profile' | grep 'another_more' | grep 'so_on' 

i tried doing same way above, edited: did work, not quite enough bring necessary data. mean, there missing data in results of search. :(

following idea of grep 'word' filename works, 1 word in mountain of data not enough. so, how pass multiple 'word' match want? want search 'id', '*some_model*' , 'account' using grep @ same time.

how do search match possible lines arguments in prompt? more doubt, possible use conditions if , else o while combining grep example?

if questions not clear, please let me know right it. all.

this question asks grep, sed or awk cleaner 'a , b'-style matches, see how run grep multiple , patterns?

this answer covers how use grep match lines match inputs - my other answer covers matching input.

note grep more powerful simple word-matching, can match arbitrary patterns, including multiple words.

consider following simplified version of example provide:

$ cat file {"id": 2796, "some_model": "profile", "was_deleted": false} {"id": 2797, "some_model": "profile", "was_deleted": true} {"id": 2798, "some_model": "another", "was_deleted": false} 

you find item 2796 so:

$ grep '"\?id"\? *: *2796 *,\?' file {"id": 2796, "some_model": "profile", "was_deleted": false} 

or find non-deleted items:

$ grep '"\?was_deleted"\? *: *false *[,}]' file {"id": 2796, "some_model": "profile", "was_deleted": false} {"id": 2798, "some_model": "another", "was_deleted": false} 

you can combine two, item 2796 if it's not deleted (change false true , line no longer matches):

$ grep '"\?id"\? *: *2796 *,\?.*"\?was_deleted"\? *: *false *[,}]' file {"id": 2796, "some_model": "profile", "was_deleted": false} 

or equivalent, using grep-piping syntax use above:

$ grep '"\?id"\? *: *2796 *,\?' file | grep '"\?was_deleted"\? *: *false *[,}]' {"id": 2796, "some_model": "profile", "was_deleted": false} 

these examples tricky right, because not idea!

the data you're working looks json, structured data format grep not suited processing. valid json split across multiple lines or have fields in arbitrary order, break above patterns. not mention arbitrary white-space (*), semi-optional quotes ("\?), , end of field vs. end of object markers ([,}]) above patterns should handle, easy wrong.

if trying query json data, need json parser, grep not. http://www.json.org/ offers links several popular json parsers in many languages, see if of suite needs. have better success real tool trying construct complicated regular expressions.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -