假设我在JSON中有一些文本:
{
"key1": "value1"
"key2": "value2"
"key3": "value3"
"key4": "value4"
"key5": "value5"
}
{
"key1": "value1"
"key2": "value2"
"key3": "value3"
"key4": "value4"
"key5": "value5"
}
我想做的是找到“ value1”,但只打印“ value5”。将有许多“ value5”,但是我只想在它前面带有“ value1”(它将是一个唯一的字符串)时打印它。我不能使用
grep -Ax & -Bx
因为我不想匹配后面的行,所以我想打印字符串。
最佳答案
示例文件:
cat t
{
"key1": "value1"
"key2": "value2"
"key3": "value3"
"key4": "value4"
"key5": "value5"
}
{
"key1": "value6"
"key2": "value2"
"key3": "value3"
"key4": "value4"
"key5": "value5"
}
如果
key5
,则在key1=value6
中打印数据awk '/key1/ && /value6/ {f=1} f && /key5/ {print $NF;f=0}'
"value5"