- Platform: RHEL7
- Shell: Bash
- Expected Result: store value as a variable
I'm trying to capture an IP from text that looks like this:
{
"ip": "12.34.56.7",
"type": "LEARNED"
}
When I run the following command from the terminal, I get the IP as expected.
grep '"ip":' ../path/to/file.txt | awk '{ print $2 }' | tr -d '"' | tr -d ','
Below is what I have in the script:
IP=grep '"ip":' ../path/to/file.txt | awk '{ print $2 }' | tr -d '"' | tr -d ','
I've tried a few different things, such as putting everything after IP=
in quotes or escaping the quotes around the grep ip text with \\
but no dice. I'm just not sure of the right method of going about this. The script either breaks apart my command if it doesn't have quotes, or for some reason it drops the $2 from awk.
Thanks for any info.
jq -r '.ip' ../path/to/file.txt
as the command iffile.txt
is actually a JSON document. – steeldriver Aug 20 '21 at 15:45