Regex in IF condition in awkRegex for matchng anything between parenthesisawk adds extra comma at several placesUsing multiple awk commands within single lineBest way to iterate through the following awk commandIf column matches another file, print every line with match (awk/grep)How do I form a new string from a parsed CSV line?Print 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 rightAwk for merging multiple files with common columnUsing awk to find matches and extract characters from BEFORE each match - help!
Performance for simple code that converts a RGB tuple to hex string
What happens if nobody can form a government in Israel?
Cut a cake into 3 equal portions with only a knife
Can the U.S. president make military decisions without consulting anyone?
A drug that allows people to survive on less food
What is the meaning of "heutig" in this sentence?
Do things made of adamantine rust?
How can I repair this gas leak on my new range? Teflon tape isn't working
How use custom order in folder on Windows 7 and 10
Which museums have artworks of all four Ninja Turtles' namesakes?
Is It Possible to Have Different Sea Levels, Eventually Causing New Landforms to Appear?
Can Northern Ireland's border issue be solved by repartition?
Is it possible to encode a message in such a way that can only be read by someone or something capable of seeing into the very near future?
Worms crawling under skin
What is the lowest voltage that a microcontroller can successfully read on the analog pin?
Is it a good idea to leave minor world details to the reader's imagination?
Manager encourages me to take day of sick leave instead of PTO, what's in it for him?
Safely hang a mirror that does not have hooks
How much Damage can be done with "just" heating matter?
Did Apollo carry and use WD40?
An Algorithm Which Schedules Your Life
Was there a trial by combat between a man and a dog in medieval France?
Do we know the situation in Britain before Sealion (summer 1940)?
How to manage expenditure when billing cycles and paycheck cycles are not aligned?
Regex in IF condition in awk
Regex for matchng anything between parenthesisawk adds extra comma at several placesUsing multiple awk commands within single lineBest way to iterate through the following awk commandIf column matches another file, print every line with match (awk/grep)How do I form a new string from a parsed CSV line?Print 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 rightAwk for merging multiple files with common columnUsing awk to find matches and extract characters from BEFORE each match - help!
.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
44.8k5 gold badges111 silver badges184 bronze badges
44.8k5 gold badges111 silver badges184 bronze badges
asked Apr 15 at 12:20
LaxmanLaxman
254 bronze badges
254 bronze badges
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/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%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
143k35 gold badges295 silver badges472 bronze badges
143k35 gold badges295 silver badges472 bronze badges
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