Regex in IF condition in awk Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionHow to use regrex with AWK for string replacement in this example?Regex for matchng anything between parenthesisawk adds extra comma at several placesawk ifs and variables - cannot pass a variable from one line towards subsequent linesUsing multiple awk commands within single lineBest way to iterate through the following awk commandawk or sed command to match regex at specific line, exit true if success, false otherwiseHow to add a condition within a for loop during substitution if string is nullPrint text before and after match, from a specific beginning and to an ending stringsearch column 2 in csv file for value, if value, then insert “invalid” and shift cells right
What does Turing mean by this statement?
Movie where a circus ringmaster turns people into animals
How does Belgium enforce obligatory attendance in elections?
Sum letters are not two different
Why is it faster to reheat something than it is to cook it?
How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?
How to write capital alpha?
Random body shuffle every night—can we still function?
Trademark violation for app?
Crossing US/Canada Border for less than 24 hours
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
How do living politicians protect their readily obtainable signatures from misuse?
Can a Beast Master ranger change beast companions?
How to pronounce 伝統色
Is there hard evidence that the grant peer review system performs significantly better than random?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Why weren't discrete x86 CPUs ever used in game hardware?
Is there public access to the Meteor Crater in Arizona?
Semigroups with no morphisms between them
Flash light on something
What is the meaning of 'breadth' in breadth first search?
How could we fake a moon landing now?
Is CEO the "profession" with the most psychopaths?
Regex in IF condition in awk
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionHow to use regrex with AWK for string replacement in this example?Regex for matchng anything between parenthesisawk adds extra comma at several placesawk ifs and variables - cannot pass a variable from one line towards subsequent linesUsing multiple awk commands within single lineBest way to iterate through the following awk commandawk or sed command to match regex at specific line, exit true if success, false otherwiseHow to add a condition within a for loop during substitution if string is nullPrint text before and after match, from a specific beginning and to an ending stringsearch column 2 in csv file for value, if value, then insert “invalid” and shift cells right
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?
Required regex condition: [[ "$1" =~ [A-Za-z] ]]
BEGIN FS = ";"; counter=0
if ((length($1) != 10 && length($1) != 12))
counter++
print counter, $1;
if ($counter -gt 2)
print "Invalid input file";
exit;
I am getting error if I use the same condition which I have posted. How to add the condition?
awk regular-expression
add a comment |
I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?
Required regex condition: [[ "$1" =~ [A-Za-z] ]]
BEGIN FS = ";"; counter=0
if ((length($1) != 10 && length($1) != 12))
counter++
print counter, $1;
if ($counter -gt 2)
print "Invalid input file";
exit;
I am getting error if I use the same condition which I have posted. How to add the condition?
awk regular-expression
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just[A-Za-z]. What are you adding to yourawk?
– terdon♦
Apr 15 at 12:34
add a comment |
I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?
Required regex condition: [[ "$1" =~ [A-Za-z] ]]
BEGIN FS = ";"; counter=0
if ((length($1) != 10 && length($1) != 12))
counter++
print counter, $1;
if ($counter -gt 2)
print "Invalid input file";
exit;
I am getting error if I use the same condition which I have posted. How to add the condition?
awk regular-expression
I have awk script file as below. I need to add another condition in the if statement to check if the string contains atleast one alphabet. How can I add the extra condition to the present if statement?
Required regex condition: [[ "$1" =~ [A-Za-z] ]]
BEGIN FS = ";"; counter=0
if ((length($1) != 10 && length($1) != 12))
counter++
print counter, $1;
if ($counter -gt 2)
print "Invalid input file";
exit;
I am getting error if I use the same condition which I have posted. How to add the condition?
awk regular-expression
awk regular-expression
edited Apr 15 at 13:05
muru
38k590166
38k590166
asked Apr 15 at 12:20
LaxmanLaxman
254
254
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just[A-Za-z]. What are you adding to yourawk?
– terdon♦
Apr 15 at 12:34
add a comment |
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just[A-Za-z]. What are you adding to yourawk?
– terdon♦
Apr 15 at 12:34
1
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just
[A-Za-z]. What are you adding to your awk?– terdon♦
Apr 15 at 12:34
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just
[A-Za-z]. What are you adding to your awk?– terdon♦
Apr 15 at 12:34
add a comment |
1 Answer
1
active
oldest
votes
You don't actually show how you add the regex, so I am guessing you are using the same format: =~ [A-Za-z]. That won't work. Each language has its own syntax for regex matching. In awk, the format is $target ~ /$regex/, so $1 ~ /[A-Za-z]/.
BEGIN FS = ";"; counter=0
if (length($1) != 10 && length($1) != 12 && $1 ~ /[A-Za-z]/)
counter++
print counter, $1;
if (counter > 2)
print "Invalid input file";
exit;
Also, in awk, the $ sign is used to mark fields, not variables. So $counter will be evaluated to the field number of counter. If counter is 2, then $counter will be the value of the second field. And the -gt is also not an awk thing. Just use >.
Yes. I was using=~format.
– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ...seems more « awkish » to me, avoiding some seriously nested braces
– D. Ben Knoble
Apr 15 at 16:53
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
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/3.0/"u003ecc by-sa 3.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%2f512567%2fregex-in-if-condition-in-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't actually show how you add the regex, so I am guessing you are using the same format: =~ [A-Za-z]. That won't work. Each language has its own syntax for regex matching. In awk, the format is $target ~ /$regex/, so $1 ~ /[A-Za-z]/.
BEGIN FS = ";"; counter=0
if (length($1) != 10 && length($1) != 12 && $1 ~ /[A-Za-z]/)
counter++
print counter, $1;
if (counter > 2)
print "Invalid input file";
exit;
Also, in awk, the $ sign is used to mark fields, not variables. So $counter will be evaluated to the field number of counter. If counter is 2, then $counter will be the value of the second field. And the -gt is also not an awk thing. Just use >.
Yes. I was using=~format.
– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ...seems more « awkish » to me, avoiding some seriously nested braces
– D. Ben Knoble
Apr 15 at 16:53
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
add a comment |
You don't actually show how you add the regex, so I am guessing you are using the same format: =~ [A-Za-z]. That won't work. Each language has its own syntax for regex matching. In awk, the format is $target ~ /$regex/, so $1 ~ /[A-Za-z]/.
BEGIN FS = ";"; counter=0
if (length($1) != 10 && length($1) != 12 && $1 ~ /[A-Za-z]/)
counter++
print counter, $1;
if (counter > 2)
print "Invalid input file";
exit;
Also, in awk, the $ sign is used to mark fields, not variables. So $counter will be evaluated to the field number of counter. If counter is 2, then $counter will be the value of the second field. And the -gt is also not an awk thing. Just use >.
Yes. I was using=~format.
– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ...seems more « awkish » to me, avoiding some seriously nested braces
– D. Ben Knoble
Apr 15 at 16:53
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
add a comment |
You don't actually show how you add the regex, so I am guessing you are using the same format: =~ [A-Za-z]. That won't work. Each language has its own syntax for regex matching. In awk, the format is $target ~ /$regex/, so $1 ~ /[A-Za-z]/.
BEGIN FS = ";"; counter=0
if (length($1) != 10 && length($1) != 12 && $1 ~ /[A-Za-z]/)
counter++
print counter, $1;
if (counter > 2)
print "Invalid input file";
exit;
Also, in awk, the $ sign is used to mark fields, not variables. So $counter will be evaluated to the field number of counter. If counter is 2, then $counter will be the value of the second field. And the -gt is also not an awk thing. Just use >.
You don't actually show how you add the regex, so I am guessing you are using the same format: =~ [A-Za-z]. That won't work. Each language has its own syntax for regex matching. In awk, the format is $target ~ /$regex/, so $1 ~ /[A-Za-z]/.
BEGIN FS = ";"; counter=0
if (length($1) != 10 && length($1) != 12 && $1 ~ /[A-Za-z]/)
counter++
print counter, $1;
if (counter > 2)
print "Invalid input file";
exit;
Also, in awk, the $ sign is used to mark fields, not variables. So $counter will be evaluated to the field number of counter. If counter is 2, then $counter will be the value of the second field. And the -gt is also not an awk thing. Just use >.
edited Apr 15 at 16:58
answered Apr 15 at 12:36
terdon♦terdon
134k33270450
134k33270450
Yes. I was using=~format.
– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ...seems more « awkish » to me, avoiding some seriously nested braces
– D. Ben Knoble
Apr 15 at 16:53
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
add a comment |
Yes. I was using=~format.
– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ...seems more « awkish » to me, avoiding some seriously nested braces
– D. Ben Knoble
Apr 15 at 16:53
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
Yes. I was using
=~ format.– Laxman
Apr 15 at 12:44
Yes. I was using
=~ format.– Laxman
Apr 15 at 12:44
Why couldnt you move the condition to be the pattern?
length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ... seems more « awkish » to me, avoiding some seriously nested braces– D. Ben Knoble
Apr 15 at 16:53
Why couldnt you move the condition to be the pattern?
length($1) != 10 && ... && $1 ~ /[A-Za-z]/ counter++ ... seems more « awkish » to me, avoiding some seriously nested braces– D. Ben Knoble
Apr 15 at 16:53
1
1
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
@D.BenKnoble why indeed! I just copy/pasted the OP's code and added the regex. I just didn't realize it was that nested. Thanks!
– terdon♦
Apr 15 at 17:00
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%2f512567%2fregex-in-if-condition-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
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
Apr 15 at 12:24
How do you add it? You are showing us a bash-style if statement. The regex is fine, but the regex is just
[A-Za-z]. What are you adding to yourawk?– terdon♦
Apr 15 at 12:34