how to use variable with awk [duplicate]

Clash 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?
linux awk variable
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.
 |Â
show 1 more comment
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?
linux awk variable
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 withjq. 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 somejqcommand to parse it.
â Kusalananda
yesterday
1
sed -n '/hive-log4j/N;s/.*n//p'
â mikeserv
yesterday
 |Â
show 1 more comment
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?
linux awk variable
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
linux awk variable
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 withjq. 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 somejqcommand to parse it.
â Kusalananda
yesterday
1
sed -n '/hive-log4j/N;s/.*n//p'
â mikeserv
yesterday
 |Â
show 1 more comment
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 withjq. 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 somejqcommand 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
 |Â
show 1 more comment
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
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
/.../ 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
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
add a comment |Â
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
add a comment |Â
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
jqcommand to parse it.â Kusalananda
yesterday
1
sed -n '/hive-log4j/N;s/.*n//p'â mikeserv
yesterday