1

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
yael
  • 13,106
  • they are absolutely different equations with different problem – yael Aug 05 '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 05 '18 at 14:28
  • can you please suggest an answer about this? – yael Aug 05 '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 05 '18 at 14:41
  • OK , I think it better to ask a new question about jq – yael Aug 05 '18 at 14:45
  • 1
    sed -n '/hive-log4j/N;s/.*\n//p' – mikeserv Aug 05 '18 at 15:02

1 Answers1

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
RudiC
  • 8,969
  • 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 05 '18 at 13:03