Reputation: 95
So I am trying to extract a specific line from multiple json files and create a txt file and add these extracted line into this txt file
Here is each json File Looks like
{
"type": "service_account",
"project_id": "mm-sxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"private_key_id": "01xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADAh\nk9P8vIQogdfdIxxxxxxx/9Fq\nwxeBX5Wk+CRXvklQQP37TDM=\n-----END PRIVATE KEY-----\n",
"client_email": "mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com",
"client_id": "1xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com"
}
Each and every json Files are having this pattern
What I am interested is in this line "client_email": "mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com",
I just need the email from that line, ie in this case mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com
I am trying to achieve this in Powershell
I tried the solution below
(Select-String -Path *json -Pattern 'client_email').Line | Set-Content AllSAEmails.txt
This created a txt file with the following pattern
"client_email": "mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com",
"client_email": "mm-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyas.iam.gserviceaccount.com",
"client_email": "mm-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzae.iam.gserviceaccount.com",
"client_email": "mm-qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqquh.iam.gserviceaccount.com"
But what I am interested is as below,is it possible to create a txt file with that pattern?
So since I have multiple jsons..each of the jsons has such an email..i would like to have all those emails inside a txt file seperated by comma
mm-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqr.iam.gserviceaccount.com,
mm-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyas.iam.gserviceaccount.com,
mm-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzae.iam.gserviceaccount.com,
mm-qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqquh.iam.gserviceaccount.com
Any help will be appreciated
Thank you
Upvotes: 0
Views: 120
Reputation: 174485
Use ConvertFrom-Json
to parse the file, then the rest becomes trivial:
$emails = Get-ChildItem .\*.json |Get-Content -Raw |ConvertFrom-Json |Select -ExpandProperty client_email
If you want to output the emails to file and insert a line break every 10 lines, you could do something like:
@(
for($i = 0;$i -lt $email.Length ;$i += 10){
# output 10 emails and a blank string
$emails[$i..($i + 9)]
""
}
) |Set-Content .\path\to\file
Upvotes: 2