Use a shell variable in awkexternal variable in awkexpanding variables in awkawk accepting variable supplied...
What is the fastest algorithm for finding the natural logarithm of a big number?
Raspberry Pi run commands on boot
How did Ron get five hundred Chocolate Frog cards?
How can AnyDVD destroy a DVD drive?
What is the word for things that work even when they aren't working (e.g. escalators)?
Did smallpox emerge in 1580?
Would a physician with migraine better treat their patients?
d-Menthol vs dl-menthol: Does an enantiomer and its racemic mixture have different melting points?
Shortest way to get an EOF Error
How to make a PCB based on ATtiny easily updatable by end users?
Proofreading a novel: is it okay to use a question mark with an exclamation mark - "?!"
Does my code handle negative numbers or zero when summing squared digits?
Why are KDFs slow? Is using a KDF more secure than using the original secret?
Skewer removal without quick release
Disrespectful employee going above my head and telling me what to do. I am his manager
If the music alphabet had more than 7 letters would octaves still sound like the same note?
Variable fixing based on a good feasible solution
Do you say "good game" after a game in which your opponent played poorly?
Can you take Bowwow out after returning him to MeowMeow?
Can I hide one of my siamese twins heads in public?
How does Firefox know my ISP login page?
Using Terminal` (ASCII plots) in Wolfram 12
Is it realistic that an advanced species isn't good at war?
How can demon technology be prevented from surpassing humans?
Use a shell variable in awk
external variable in awkexpanding variables in awkawk accepting variable supplied by dollar sign in cshellHow can I multiply a column by a variable and write out?redirect awk exec results to another folderHow can I prepend the value of a variable to a column in bash?cycle using awkRun unix command on awk fieldCreating a new file inside awk but in different directory based on input field valueDefine a variable to awk pattern matching from bashbash - can I do : find … -exec this && that?Why does AWK not like dots in one of two substitutions?BEGIN and END with the awk commandValue assigned inside a function variable is always emptyHow to insert bash variables in awk?awk ifs and variables - cannot pass a variable from one line towards subsequent linesVariable Capture and For-Loop Arithmeticawk a variable as a regexMultiline Regexp (grep, sed, awk, perl)find inside shell function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
Here is my script (to find the files that contain a specified pattern):
find . -type f
-exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' {} ;
I would like to use my script with an argument §:
MyScript.sh pattern
My problem is that I don't manage to put the $1
variable in awk
.
When I try to debug my script
bash -x MyScript.sh pattern
Here is the output :
+ find . -type f -exec awk -v vawk=pattern '// {c++} c>0 {print ARGV[1] ; exit 0 } END { if (! c) {exit 1}}' '{}' ';'
The $vawk
variable seems to be empty.
Any idea?
bash awk variable
add a comment
|
Here is my script (to find the files that contain a specified pattern):
find . -type f
-exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' {} ;
I would like to use my script with an argument §:
MyScript.sh pattern
My problem is that I don't manage to put the $1
variable in awk
.
When I try to debug my script
bash -x MyScript.sh pattern
Here is the output :
+ find . -type f -exec awk -v vawk=pattern '// {c++} c>0 {print ARGV[1] ; exit 0 } END { if (! c) {exit 1}}' '{}' ';'
The $vawk
variable seems to be empty.
Any idea?
bash awk variable
add a comment
|
Here is my script (to find the files that contain a specified pattern):
find . -type f
-exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' {} ;
I would like to use my script with an argument §:
MyScript.sh pattern
My problem is that I don't manage to put the $1
variable in awk
.
When I try to debug my script
bash -x MyScript.sh pattern
Here is the output :
+ find . -type f -exec awk -v vawk=pattern '// {c++} c>0 {print ARGV[1] ; exit 0 } END { if (! c) {exit 1}}' '{}' ';'
The $vawk
variable seems to be empty.
Any idea?
bash awk variable
Here is my script (to find the files that contain a specified pattern):
find . -type f
-exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' {} ;
I would like to use my script with an argument §:
MyScript.sh pattern
My problem is that I don't manage to put the $1
variable in awk
.
When I try to debug my script
bash -x MyScript.sh pattern
Here is the output :
+ find . -type f -exec awk -v vawk=pattern '// {c++} c>0 {print ARGV[1] ; exit 0 } END { if (! c) {exit 1}}' '{}' ';'
The $vawk
variable seems to be empty.
Any idea?
bash awk variable
bash awk variable
edited Mar 23 at 7:57
Kusalananda♦
167k20 gold badges325 silver badges522 bronze badges
167k20 gold badges325 silver badges522 bronze badges
asked Oct 5 '12 at 18:26
NicolasNicolas
2292 silver badges11 bronze badges
2292 silver badges11 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
You seem to be confusing awk variables and shell variables. awk -v vawk="$1"
creates an awk variable called vawk
, yet you are trying to use shell syntax ($vawk
). This doesn't work because the shell doesn't have a variable called vawk
. I think what you want is
awk -v vawk="$1" '$0 ~ vawk { c++ } # ...'
# ^ awk variable syntax
1
Note thatawk
expands the C-like escape sequences in$1
, so that approach doesn't work if$1
may contain backslash characters (common for regexps). You may use theENVIRON
awk special array instead.
– Stéphane Chazelas
Apr 18 '17 at 12:27
add a comment
|
Reproduced from this now closed as duplicate question as it includes warnings on the limitations of awk variable passing which one might find useful.
A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:
awk -v x="$x" '$2 == x {print $1}' infile
or
awk '$2 == x {print $1}' x="$x" infile
However, those suffer from a problem: escape sequences are expanded in them.
So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.
Another approach (but which like for -v
requires a POSIX awk or nawk (as opposed to the 1970's awk still found as /bin/awk
in Solaris)) is to use environment variables:
x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile
Another approach (still with newer awks) is to use the ARGV array in awk:
awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
$2 == x {print $1}' "$x" infile
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f50044%2fuse-a-shell-variable-in-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You seem to be confusing awk variables and shell variables. awk -v vawk="$1"
creates an awk variable called vawk
, yet you are trying to use shell syntax ($vawk
). This doesn't work because the shell doesn't have a variable called vawk
. I think what you want is
awk -v vawk="$1" '$0 ~ vawk { c++ } # ...'
# ^ awk variable syntax
1
Note thatawk
expands the C-like escape sequences in$1
, so that approach doesn't work if$1
may contain backslash characters (common for regexps). You may use theENVIRON
awk special array instead.
– Stéphane Chazelas
Apr 18 '17 at 12:27
add a comment
|
You seem to be confusing awk variables and shell variables. awk -v vawk="$1"
creates an awk variable called vawk
, yet you are trying to use shell syntax ($vawk
). This doesn't work because the shell doesn't have a variable called vawk
. I think what you want is
awk -v vawk="$1" '$0 ~ vawk { c++ } # ...'
# ^ awk variable syntax
1
Note thatawk
expands the C-like escape sequences in$1
, so that approach doesn't work if$1
may contain backslash characters (common for regexps). You may use theENVIRON
awk special array instead.
– Stéphane Chazelas
Apr 18 '17 at 12:27
add a comment
|
You seem to be confusing awk variables and shell variables. awk -v vawk="$1"
creates an awk variable called vawk
, yet you are trying to use shell syntax ($vawk
). This doesn't work because the shell doesn't have a variable called vawk
. I think what you want is
awk -v vawk="$1" '$0 ~ vawk { c++ } # ...'
# ^ awk variable syntax
You seem to be confusing awk variables and shell variables. awk -v vawk="$1"
creates an awk variable called vawk
, yet you are trying to use shell syntax ($vawk
). This doesn't work because the shell doesn't have a variable called vawk
. I think what you want is
awk -v vawk="$1" '$0 ~ vawk { c++ } # ...'
# ^ awk variable syntax
edited Oct 5 '12 at 18:37
answered Oct 5 '12 at 18:31
jw013jw013
38.8k8 gold badges107 silver badges127 bronze badges
38.8k8 gold badges107 silver badges127 bronze badges
1
Note thatawk
expands the C-like escape sequences in$1
, so that approach doesn't work if$1
may contain backslash characters (common for regexps). You may use theENVIRON
awk special array instead.
– Stéphane Chazelas
Apr 18 '17 at 12:27
add a comment
|
1
Note thatawk
expands the C-like escape sequences in$1
, so that approach doesn't work if$1
may contain backslash characters (common for regexps). You may use theENVIRON
awk special array instead.
– Stéphane Chazelas
Apr 18 '17 at 12:27
1
1
Note that
awk
expands the C-like escape sequences in $1
, so that approach doesn't work if $1
may contain backslash characters (common for regexps). You may use the ENVIRON
awk special array instead.– Stéphane Chazelas
Apr 18 '17 at 12:27
Note that
awk
expands the C-like escape sequences in $1
, so that approach doesn't work if $1
may contain backslash characters (common for regexps). You may use the ENVIRON
awk special array instead.– Stéphane Chazelas
Apr 18 '17 at 12:27
add a comment
|
Reproduced from this now closed as duplicate question as it includes warnings on the limitations of awk variable passing which one might find useful.
A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:
awk -v x="$x" '$2 == x {print $1}' infile
or
awk '$2 == x {print $1}' x="$x" infile
However, those suffer from a problem: escape sequences are expanded in them.
So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.
Another approach (but which like for -v
requires a POSIX awk or nawk (as opposed to the 1970's awk still found as /bin/awk
in Solaris)) is to use environment variables:
x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile
Another approach (still with newer awks) is to use the ARGV array in awk:
awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
$2 == x {print $1}' "$x" infile
add a comment
|
Reproduced from this now closed as duplicate question as it includes warnings on the limitations of awk variable passing which one might find useful.
A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:
awk -v x="$x" '$2 == x {print $1}' infile
or
awk '$2 == x {print $1}' x="$x" infile
However, those suffer from a problem: escape sequences are expanded in them.
So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.
Another approach (but which like for -v
requires a POSIX awk or nawk (as opposed to the 1970's awk still found as /bin/awk
in Solaris)) is to use environment variables:
x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile
Another approach (still with newer awks) is to use the ARGV array in awk:
awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
$2 == x {print $1}' "$x" infile
add a comment
|
Reproduced from this now closed as duplicate question as it includes warnings on the limitations of awk variable passing which one might find useful.
A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:
awk -v x="$x" '$2 == x {print $1}' infile
or
awk '$2 == x {print $1}' x="$x" infile
However, those suffer from a problem: escape sequences are expanded in them.
So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.
Another approach (but which like for -v
requires a POSIX awk or nawk (as opposed to the 1970's awk still found as /bin/awk
in Solaris)) is to use environment variables:
x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile
Another approach (still with newer awks) is to use the ARGV array in awk:
awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
$2 == x {print $1}' "$x" infile
Reproduced from this now closed as duplicate question as it includes warnings on the limitations of awk variable passing which one might find useful.
A shell variable is just that: a shell variable. If you want to turn it into a awk variable, you need a syntax such as:
awk -v x="$x" '$2 == x {print $1}' infile
or
awk '$2 == x {print $1}' x="$x" infile
However, those suffer from a problem: escape sequences are expanded in them.
So, for instance if the shell variable contains the two characters backslash and n, the awk variable will end up containing the newline character.
Another approach (but which like for -v
requires a POSIX awk or nawk (as opposed to the 1970's awk still found as /bin/awk
in Solaris)) is to use environment variables:
x="$x" awk '$2 == ENVIRON["x"] {print $1}' infile
Another approach (still with newer awks) is to use the ARGV array in awk:
awk 'BEGIN {x = ARGV[1]; delete ARGV[1]}
$2 == x {print $1}' "$x" infile
edited Mar 23 at 9:26
answered Nov 20 '12 at 10:44
Stéphane ChazelasStéphane Chazelas
337k58 gold badges658 silver badges1037 bronze badges
337k58 gold badges658 silver badges1037 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f50044%2fuse-a-shell-variable-in-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown