Can I grep for 2 pattterns and have them listed side by side?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
3












Ubuntu 16.04

bash -version

GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)



I would like to grep for 2 patterns and then have them listed side by side. ATM, this is what I have:



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json: "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json: "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


THis is what I would like to have or anything similar would work actually.



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json: tire_id: 1815123428733289, appID: 18412365908966538


File example here:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": XXXX,
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD",
,
"attachments":
"output": "attachments",
"public": false,
,







share|improve this question

















  • 4




    Looks like something more apt for jq than grep.
    – DopeGhoti
    Jul 12 at 15:25






  • 1




    post a testable production.json contents
    – RomanPerekhrest
    Jul 12 at 15:29










  • @RomanPerekhrest I posted an example, see OP.
    – needtoknow
    Jul 12 at 16:07














up vote
3
down vote

favorite
3












Ubuntu 16.04

bash -version

GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)



I would like to grep for 2 patterns and then have them listed side by side. ATM, this is what I have:



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json: "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json: "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


THis is what I would like to have or anything similar would work actually.



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json: tire_id: 1815123428733289, appID: 18412365908966538


File example here:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": XXXX,
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD",
,
"attachments":
"output": "attachments",
"public": false,
,







share|improve this question

















  • 4




    Looks like something more apt for jq than grep.
    – DopeGhoti
    Jul 12 at 15:25






  • 1




    post a testable production.json contents
    – RomanPerekhrest
    Jul 12 at 15:29










  • @RomanPerekhrest I posted an example, see OP.
    – needtoknow
    Jul 12 at 16:07












up vote
3
down vote

favorite
3









up vote
3
down vote

favorite
3






3





Ubuntu 16.04

bash -version

GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)



I would like to grep for 2 patterns and then have them listed side by side. ATM, this is what I have:



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json: "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json: "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


THis is what I would like to have or anything similar would work actually.



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json: tire_id: 1815123428733289, appID: 18412365908966538


File example here:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": XXXX,
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD",
,
"attachments":
"output": "attachments",
"public": false,
,







share|improve this question













Ubuntu 16.04

bash -version

GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu)



I would like to grep for 2 patterns and then have them listed side by side. ATM, this is what I have:



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json: "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json: "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


THis is what I would like to have or anything similar would work actually.



root@tires ~ # grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json: tire_id: 1305436516186552, appID: 1164562920689523
/path/to/000001_000079/vehicle/production.json: tire_id: 1815123428733289, appID: 18412365908966538


File example here:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": XXXX,
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD",
,
"attachments":
"output": "attachments",
"public": false,
,









share|improve this question












share|improve this question




share|improve this question








edited Jul 12 at 16:07
























asked Jul 12 at 15:19









needtoknow

1286




1286







  • 4




    Looks like something more apt for jq than grep.
    – DopeGhoti
    Jul 12 at 15:25






  • 1




    post a testable production.json contents
    – RomanPerekhrest
    Jul 12 at 15:29










  • @RomanPerekhrest I posted an example, see OP.
    – needtoknow
    Jul 12 at 16:07












  • 4




    Looks like something more apt for jq than grep.
    – DopeGhoti
    Jul 12 at 15:25






  • 1




    post a testable production.json contents
    – RomanPerekhrest
    Jul 12 at 15:29










  • @RomanPerekhrest I posted an example, see OP.
    – needtoknow
    Jul 12 at 16:07







4




4




Looks like something more apt for jq than grep.
– DopeGhoti
Jul 12 at 15:25




Looks like something more apt for jq than grep.
– DopeGhoti
Jul 12 at 15:25




1




1




post a testable production.json contents
– RomanPerekhrest
Jul 12 at 15:29




post a testable production.json contents
– RomanPerekhrest
Jul 12 at 15:29












@RomanPerekhrest I posted an example, see OP.
– needtoknow
Jul 12 at 16:07




@RomanPerekhrest I posted an example, see OP.
– needtoknow
Jul 12 at 16:07










4 Answers
4






active

oldest

votes

















up vote
11
down vote



accepted










The right way with jq tool for a valid JSON documents:



Sample file1.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





Sample file2.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "1305436516186552",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "1164562920689523",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





And the solution itself:



jq -r 'input_filename + " tire_id: (.tire_id) appID: (.cdc.appID)"' file*.json


The output:



file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523





share|improve this answer





















  • Your answer is much simpler and efficient for what I needed.
    – needtoknow
    Jul 12 at 17:40

















up vote
1
down vote













You can do this with grep but it's rather conplicated and Roman's answer is actually better.



Your example contents are just from one file so there is only once instance of tire_id and appID.



Use this:



echo $(echo /path/to/production.json && egrep "tire_id|appID" /path/to/production.json | sed -e 's|"||g' | sed -e 's|,||2') && echo $(echo /path/to/production2.json && egrep "tire_id|appID" /path/to/production2.json ) | sed -e 's|"||g' | sed -e 's|,||2'


echo puts everything on the same line with command substitution.



egrep does the same thing as grep -e but allows you to put all the strings together separated by | instead of having to use -e string each time.



sed -e 's|"||g' removes the apostrophes.



sed -e 's|,||2 removes the comma from the end of the value for appID which is what is shown in your desired output.



Output:



/path/to/production.json tire_id: 213275925375485 appID: 233262274090443
/path/to/production2.json tire_id: 1815123428733289 appID: 18412365908966538


/path/to/production.json and /path/to/production2.json are just placeholders.



This obviously requires a lot of modification as you'd have to grep each file separately and use an echo command substitution for each one. I'm only including it if you insist upon using grep or if in the future, it's not a json file.






share|improve this answer























  • @Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
    – needtoknow
    Jul 12 at 17:40






  • 1




    @needtoknow No problem. Glad to help.
    – Nasir Riley
    Jul 12 at 17:48

















up vote
0
down vote













With a sed script :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n '/(.*:)/h;n;s/.*n://;H;g;s/n//;p'


Retrun



/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552", "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289", "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


it works if each line "tire_id" is followed by a line "appID".
Otherwise, you need a more complex sed script






share|improve this answer























  • This is also nice. Can you remove the apostrophes and commas.
    – needtoknow
    Jul 12 at 20:01










  • sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
    – alux
    Jul 12 at 21:09

















up vote
0
down vote













This command will work even if in a file it misses a "tire_id" or "appID" line. It will not be displayed :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n 's/(tire_id)/1/;T;h;n;s/.*n:.*(appID)/1/;T;H;g;s/n/ /;s/"//g;s/,//g;p'





share|improve this answer





















  • Very cool mate. I learned a lot from it.
    – needtoknow
    Jul 12 at 22:22










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454928%2fcan-i-grep-for-2-pattterns-and-have-them-listed-side-by-side%23new-answer', 'question_page');

);

Post as a guest






























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
11
down vote



accepted










The right way with jq tool for a valid JSON documents:



Sample file1.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





Sample file2.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "1305436516186552",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "1164562920689523",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





And the solution itself:



jq -r 'input_filename + " tire_id: (.tire_id) appID: (.cdc.appID)"' file*.json


The output:



file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523





share|improve this answer





















  • Your answer is much simpler and efficient for what I needed.
    – needtoknow
    Jul 12 at 17:40














up vote
11
down vote



accepted










The right way with jq tool for a valid JSON documents:



Sample file1.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





Sample file2.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "1305436516186552",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "1164562920689523",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





And the solution itself:



jq -r 'input_filename + " tire_id: (.tire_id) appID: (.cdc.appID)"' file*.json


The output:



file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523





share|improve this answer





















  • Your answer is much simpler and efficient for what I needed.
    – needtoknow
    Jul 12 at 17:40












up vote
11
down vote



accepted







up vote
11
down vote



accepted






The right way with jq tool for a valid JSON documents:



Sample file1.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





Sample file2.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "1305436516186552",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "1164562920689523",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





And the solution itself:



jq -r 'input_filename + " tire_id: (.tire_id) appID: (.cdc.appID)"' file*.json


The output:



file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523





share|improve this answer













The right way with jq tool for a valid JSON documents:



Sample file1.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "213275925375485",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "233262274090443",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





Sample file2.json:




"socal": "https://xxx.xxxxx.xxx",
"ip": "xxx.xxx.xxx.xxx",
"tire_id": "1305436516186552",
"client":
"platform": "xx",
"clientID": "xxxxx",
"serviceID": "xxxxx",
"service_id": "XXXX",
"vendor": "default"
,
"locale": "en_US",
"cdc":
"appID": "1164562920689523",
"isdel": "ORdiZBMAQS2ZBCnTwZDZD"
,
"attachments":
"output": "attachments",
"public": false





And the solution itself:



jq -r 'input_filename + " tire_id: (.tire_id) appID: (.cdc.appID)"' file*.json


The output:



file1.json tire_id: 213275925375485 appID: 233262274090443
file2.json tire_id: 1305436516186552 appID: 1164562920689523






share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 12 at 16:37









RomanPerekhrest

22.4k12144




22.4k12144











  • Your answer is much simpler and efficient for what I needed.
    – needtoknow
    Jul 12 at 17:40
















  • Your answer is much simpler and efficient for what I needed.
    – needtoknow
    Jul 12 at 17:40















Your answer is much simpler and efficient for what I needed.
– needtoknow
Jul 12 at 17:40




Your answer is much simpler and efficient for what I needed.
– needtoknow
Jul 12 at 17:40












up vote
1
down vote













You can do this with grep but it's rather conplicated and Roman's answer is actually better.



Your example contents are just from one file so there is only once instance of tire_id and appID.



Use this:



echo $(echo /path/to/production.json && egrep "tire_id|appID" /path/to/production.json | sed -e 's|"||g' | sed -e 's|,||2') && echo $(echo /path/to/production2.json && egrep "tire_id|appID" /path/to/production2.json ) | sed -e 's|"||g' | sed -e 's|,||2'


echo puts everything on the same line with command substitution.



egrep does the same thing as grep -e but allows you to put all the strings together separated by | instead of having to use -e string each time.



sed -e 's|"||g' removes the apostrophes.



sed -e 's|,||2 removes the comma from the end of the value for appID which is what is shown in your desired output.



Output:



/path/to/production.json tire_id: 213275925375485 appID: 233262274090443
/path/to/production2.json tire_id: 1815123428733289 appID: 18412365908966538


/path/to/production.json and /path/to/production2.json are just placeholders.



This obviously requires a lot of modification as you'd have to grep each file separately and use an echo command substitution for each one. I'm only including it if you insist upon using grep or if in the future, it's not a json file.






share|improve this answer























  • @Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
    – needtoknow
    Jul 12 at 17:40






  • 1




    @needtoknow No problem. Glad to help.
    – Nasir Riley
    Jul 12 at 17:48














up vote
1
down vote













You can do this with grep but it's rather conplicated and Roman's answer is actually better.



Your example contents are just from one file so there is only once instance of tire_id and appID.



Use this:



echo $(echo /path/to/production.json && egrep "tire_id|appID" /path/to/production.json | sed -e 's|"||g' | sed -e 's|,||2') && echo $(echo /path/to/production2.json && egrep "tire_id|appID" /path/to/production2.json ) | sed -e 's|"||g' | sed -e 's|,||2'


echo puts everything on the same line with command substitution.



egrep does the same thing as grep -e but allows you to put all the strings together separated by | instead of having to use -e string each time.



sed -e 's|"||g' removes the apostrophes.



sed -e 's|,||2 removes the comma from the end of the value for appID which is what is shown in your desired output.



Output:



/path/to/production.json tire_id: 213275925375485 appID: 233262274090443
/path/to/production2.json tire_id: 1815123428733289 appID: 18412365908966538


/path/to/production.json and /path/to/production2.json are just placeholders.



This obviously requires a lot of modification as you'd have to grep each file separately and use an echo command substitution for each one. I'm only including it if you insist upon using grep or if in the future, it's not a json file.






share|improve this answer























  • @Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
    – needtoknow
    Jul 12 at 17:40






  • 1




    @needtoknow No problem. Glad to help.
    – Nasir Riley
    Jul 12 at 17:48












up vote
1
down vote










up vote
1
down vote









You can do this with grep but it's rather conplicated and Roman's answer is actually better.



Your example contents are just from one file so there is only once instance of tire_id and appID.



Use this:



echo $(echo /path/to/production.json && egrep "tire_id|appID" /path/to/production.json | sed -e 's|"||g' | sed -e 's|,||2') && echo $(echo /path/to/production2.json && egrep "tire_id|appID" /path/to/production2.json ) | sed -e 's|"||g' | sed -e 's|,||2'


echo puts everything on the same line with command substitution.



egrep does the same thing as grep -e but allows you to put all the strings together separated by | instead of having to use -e string each time.



sed -e 's|"||g' removes the apostrophes.



sed -e 's|,||2 removes the comma from the end of the value for appID which is what is shown in your desired output.



Output:



/path/to/production.json tire_id: 213275925375485 appID: 233262274090443
/path/to/production2.json tire_id: 1815123428733289 appID: 18412365908966538


/path/to/production.json and /path/to/production2.json are just placeholders.



This obviously requires a lot of modification as you'd have to grep each file separately and use an echo command substitution for each one. I'm only including it if you insist upon using grep or if in the future, it's not a json file.






share|improve this answer















You can do this with grep but it's rather conplicated and Roman's answer is actually better.



Your example contents are just from one file so there is only once instance of tire_id and appID.



Use this:



echo $(echo /path/to/production.json && egrep "tire_id|appID" /path/to/production.json | sed -e 's|"||g' | sed -e 's|,||2') && echo $(echo /path/to/production2.json && egrep "tire_id|appID" /path/to/production2.json ) | sed -e 's|"||g' | sed -e 's|,||2'


echo puts everything on the same line with command substitution.



egrep does the same thing as grep -e but allows you to put all the strings together separated by | instead of having to use -e string each time.



sed -e 's|"||g' removes the apostrophes.



sed -e 's|,||2 removes the comma from the end of the value for appID which is what is shown in your desired output.



Output:



/path/to/production.json tire_id: 213275925375485 appID: 233262274090443
/path/to/production2.json tire_id: 1815123428733289 appID: 18412365908966538


/path/to/production.json and /path/to/production2.json are just placeholders.



This obviously requires a lot of modification as you'd have to grep each file separately and use an echo command substitution for each one. I'm only including it if you insist upon using grep or if in the future, it's not a json file.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 13 at 1:10


























answered Jul 12 at 16:36









Nasir Riley

1,484138




1,484138











  • @Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
    – needtoknow
    Jul 12 at 17:40






  • 1




    @needtoknow No problem. Glad to help.
    – Nasir Riley
    Jul 12 at 17:48
















  • @Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
    – needtoknow
    Jul 12 at 17:40






  • 1




    @needtoknow No problem. Glad to help.
    – Nasir Riley
    Jul 12 at 17:48















@Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
– needtoknow
Jul 12 at 17:40




@Your answer is awesome and I learned a lot from it. Much appreciated. You are also right about Roman's answer. He uses the command made for the job, which I did not know existed. Hat's off to you mate.
– needtoknow
Jul 12 at 17:40




1




1




@needtoknow No problem. Glad to help.
– Nasir Riley
Jul 12 at 17:48




@needtoknow No problem. Glad to help.
– Nasir Riley
Jul 12 at 17:48










up vote
0
down vote













With a sed script :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n '/(.*:)/h;n;s/.*n://;H;g;s/n//;p'


Retrun



/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552", "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289", "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


it works if each line "tire_id" is followed by a line "appID".
Otherwise, you need a more complex sed script






share|improve this answer























  • This is also nice. Can you remove the apostrophes and commas.
    – needtoknow
    Jul 12 at 20:01










  • sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
    – alux
    Jul 12 at 21:09














up vote
0
down vote













With a sed script :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n '/(.*:)/h;n;s/.*n://;H;g;s/n//;p'


Retrun



/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552", "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289", "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


it works if each line "tire_id" is followed by a line "appID".
Otherwise, you need a more complex sed script






share|improve this answer























  • This is also nice. Can you remove the apostrophes and commas.
    – needtoknow
    Jul 12 at 20:01










  • sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
    – alux
    Jul 12 at 21:09












up vote
0
down vote










up vote
0
down vote









With a sed script :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n '/(.*:)/h;n;s/.*n://;H;g;s/n//;p'


Retrun



/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552", "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289", "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


it works if each line "tire_id" is followed by a line "appID".
Otherwise, you need a more complex sed script






share|improve this answer















With a sed script :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n '/(.*:)/h;n;s/.*n://;H;g;s/n//;p'


Retrun



/path/to/000001_000002/vehicle/production.json: "tire_id": "1305436516186552", "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json: "tire_id": "1815123428733289", "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json: "tire_id": "138477888324",


it works if each line "tire_id" is followed by a line "appID".
Otherwise, you need a more complex sed script







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 12 at 18:28


























answered Jul 12 at 18:17









alux

364




364











  • This is also nice. Can you remove the apostrophes and commas.
    – needtoknow
    Jul 12 at 20:01










  • sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
    – alux
    Jul 12 at 21:09
















  • This is also nice. Can you remove the apostrophes and commas.
    – needtoknow
    Jul 12 at 20:01










  • sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
    – alux
    Jul 12 at 21:09















This is also nice. Can you remove the apostrophes and commas.
– needtoknow
Jul 12 at 20:01




This is also nice. Can you remove the apostrophes and commas.
– needtoknow
Jul 12 at 20:01












sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
– alux
Jul 12 at 21:09




sed -n 'h;n;s/.*n://;H;g;s/n//;s/"//g;s/,//g;p'
– alux
Jul 12 at 21:09










up vote
0
down vote













This command will work even if in a file it misses a "tire_id" or "appID" line. It will not be displayed :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n 's/(tire_id)/1/;T;h;n;s/.*n:.*(appID)/1/;T;H;g;s/n/ /;s/"//g;s/,//g;p'





share|improve this answer





















  • Very cool mate. I learned a lot from it.
    – needtoknow
    Jul 12 at 22:22














up vote
0
down vote













This command will work even if in a file it misses a "tire_id" or "appID" line. It will not be displayed :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n 's/(tire_id)/1/;T;h;n;s/.*n:.*(appID)/1/;T;H;g;s/n/ /;s/"//g;s/,//g;p'





share|improve this answer





















  • Very cool mate. I learned a lot from it.
    – needtoknow
    Jul 12 at 22:22












up vote
0
down vote










up vote
0
down vote









This command will work even if in a file it misses a "tire_id" or "appID" line. It will not be displayed :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n 's/(tire_id)/1/;T;h;n;s/.*n:.*(appID)/1/;T;H;g;s/n/ /;s/"//g;s/,//g;p'





share|improve this answer













This command will work even if in a file it misses a "tire_id" or "appID" line. It will not be displayed :



grep -e tire_id -e appID /path/to/*/vehicle/production.json | sed -n 's/(tire_id)/1/;T;h;n;s/.*n:.*(appID)/1/;T;H;g;s/n/ /;s/"//g;s/,//g;p'






share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 12 at 21:39









alux

364




364











  • Very cool mate. I learned a lot from it.
    – needtoknow
    Jul 12 at 22:22
















  • Very cool mate. I learned a lot from it.
    – needtoknow
    Jul 12 at 22:22















Very cool mate. I learned a lot from it.
– needtoknow
Jul 12 at 22:22




Very cool mate. I learned a lot from it.
– needtoknow
Jul 12 at 22:22












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454928%2fcan-i-grep-for-2-pattterns-and-have-them-listed-side-by-side%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos