1

This question already has an answer here:

we have the following file

 cat  /tmp/hive.conf

      "hive-exec-log4j2" : {
        "tag" : "TOPOLOGY_RESOLVED",
        "version" : 2
      },
      "hive-interactive-env" : {
        "tag" : "TOPOLOGY_RESOLVED",
        "version" : 2
      },
      "hive-interactive-site" : {
        "tag" : "TOPOLOGY_RESOLVED",
        "version" : 2
      },
      "hive-log4j" : {
        "tag" : "TOPOLOGY_RESOLVED",
        "version" : 2
      },
      "hive-log4j2" : {
        "tag" : "TOPOLOGY_RESOLVED",
        "version" : 2
      },

we want to capture the line after match the "hive-log4j" from file

so we get that:

cat  /tmp/hive.conf |  awk  '/"hive-log4j"/{getline; print}'
        "tag" : "TOPOLOGY_RESOLVED",

now we want to do the same with awk and export the variable as the following

 val="hive-log4j"
 cat  /tmp/hive.conf |  awk -v var=$val '/"var"/{getline; print}' 

but no output

what is wrong with my syntax?

marked as duplicate by steeldriver, Jeff Schaller, roaima, schily, Thomas Aug 5 '18 at 13:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • they are absolutely different equations with different problem – yael Aug 5 '18 at 12:51
  • It would have been better if the conf file was a complete JSON document, in which case it would have been trivial to parse it with jq. It looks as if it's been proprocessed and broken in the processing. – Kusalananda Aug 5 '18 at 14:28
  • can you please suggest an answer about this? – yael Aug 5 '18 at 14:39
  • No, because the question is closed, and because the JSON in the configuration file is broken. If you update the question, it will be put in the "reopen queue", and if the format of the configuration file turns out to be properly formatted JSON, I would vote to reopen it, and then suggest some jq command to parse it. – Kusalananda Aug 5 '18 at 14:41
  • 1
    sed -n '/hive-log4j/N;s/.*\n//p' – mikeserv Aug 5 '18 at 15:02
0

/.../ is a sort of regex constant, trying to match the string "var", not var's contents. Try

awk -v var=$val '$0 ~ var {getline; print}' file

or

awk -v var=$val 'match ($0, var) {getline; print}' file

Make sure the shell variable contains the double quotes as they are part of the pattern. If that's not possible, try

awk -v var=$val 'match ($0, "\"" var "\"") {getline; print}' file
  • this isnt work as I ask , because its print both "tag" : "TOPOLOGY_RESOLVED", "tag" : "TOPOLOGY_RESOLVED", instead to print only the line under "hive-log4j" – yael Aug 5 '18 at 13:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.