I have the following output from my command. I am trying to extract the id from the text if possible as a one liner
# module.mongodb_atlas.mongodbatlas_project_ip_whitelist.vpc-whitelist["10.3.0.3/17"]:
resource "mongodbatlas_project_ip_whitelist" "vpc-whitelist" {
cidr_block = "10.3.0.0/17"
comment = "External access from '10.3.0.3/17' to the MongoDB Atlas"
id = "dffgsgsnk=:sdgsdg=-cHJvamVjdF9pZA==:sdfsdfs"
project_id = "sdsdss"
timeouts {}
}
I am trying to get the id from the above text using bash.
I tried converting the output to json but it did not work as the output can have different formats which would make it difficult.
echo $json| tail -n 3 | sed 's/[[:blank:]]//g'
i removed the empty space. But the sed part of extracting the id= hasnt worked.
echo $json| tail -n 3 | sed 's/[[:blank:]]//g' | sed -e '/id=/!d'
I also tried different ways of grep to get the id but nothing worked. Everything gave an empty string. grep
with id would give me 3 lines as output so i need id= to be the matching string
echo $json| tail -n 3 | sed 's/[[:blank:]]//g' | grep 'project_id'
project_id="dfgdfg"
echo $json| tail -n 3 | sed 's/[[:blank:]]//g' | grep '="'
(Once i add id="
here nothing comes up)
cidr_block="10.3.0.3/17" comment="Externalaccessfrom'10.3.0.3/17'totheMongoDBAtlas" id="ZW50cnk=:fdgdfg=-dfg==:dg" project_id="dfgdfg"
CodePudding user response:
Using sed
$ sed -n '/\<id\>/ {s/[^"]*"\([^"]*\).*/\1/p}' <<< "$json"
dffgsgsnk=:sdgsdg=-cHJvamVjdF9pZA==:sdfsdfs
CodePudding user response:
using awk, without converting it to json:
awk '/\yid\y/ { gsub(/"/, "");print $3}' file
/\yid\y/
exact match of the id wordgsub(/"/, "")
quotes removalprint $3
print the id field