how to use variable with awk [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite













This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



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?







share|improve this question













marked as duplicate by steeldriver, Jeff Schaller, roaima, schily, Thomas yesterday


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
    yesterday










  • 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
    yesterday










  • can you please suggest an answer about this?
    – yael
    yesterday










  • 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
    yesterday







  • 1




    sed -n '/hive-log4j/N;s/.*n//p'
    – mikeserv
    yesterday
















up vote
1
down vote

favorite













This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



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?







share|improve this question













marked as duplicate by steeldriver, Jeff Schaller, roaima, schily, Thomas yesterday


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
    yesterday










  • 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
    yesterday










  • can you please suggest an answer about this?
    – yael
    yesterday










  • 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
    yesterday







  • 1




    sed -n '/hive-log4j/N;s/.*n//p'
    – mikeserv
    yesterday












up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



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?







share|improve this question














This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



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?





This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers









share|improve this question












share|improve this question




share|improve this question








edited yesterday









Jeff Schaller

30.7k846104




30.7k846104









asked yesterday









yael

1,786940




1,786940




marked as duplicate by steeldriver, Jeff Schaller, roaima, schily, Thomas yesterday


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.






marked as duplicate by steeldriver, Jeff Schaller, roaima, schily, Thomas yesterday


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
    yesterday










  • 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
    yesterday










  • can you please suggest an answer about this?
    – yael
    yesterday










  • 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
    yesterday







  • 1




    sed -n '/hive-log4j/N;s/.*n//p'
    – mikeserv
    yesterday
















  • they are absolutely different equations with different problem
    – yael
    yesterday










  • 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
    yesterday










  • can you please suggest an answer about this?
    – yael
    yesterday










  • 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
    yesterday







  • 1




    sed -n '/hive-log4j/N;s/.*n//p'
    – mikeserv
    yesterday















they are absolutely different equations with different problem
– yael
yesterday




they are absolutely different equations with different problem
– yael
yesterday












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
yesterday




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
yesterday












can you please suggest an answer about this?
– yael
yesterday




can you please suggest an answer about this?
– yael
yesterday












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
yesterday





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
yesterday





1




1




sed -n '/hive-log4j/N;s/.*n//p'
– mikeserv
yesterday




sed -n '/hive-log4j/N;s/.*n//p'
– mikeserv
yesterday










1 Answer
1






active

oldest

votes

















up vote
0
down vote













/.../ 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





share|improve this answer























  • 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
    yesterday


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













/.../ 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





share|improve this answer























  • 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
    yesterday















up vote
0
down vote













/.../ 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





share|improve this answer























  • 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
    yesterday













up vote
0
down vote










up vote
0
down vote









/.../ 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





share|improve this answer















/.../ 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






share|improve this answer















share|improve this answer



share|improve this answer








edited yesterday


























answered yesterday









RudiC

612




612











  • 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
    yesterday

















  • 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
    yesterday
















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
yesterday





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
yesterday