Regex in IF condition in awk Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 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
Why did the IBM 650 use bi-quinary?
Why does Python start at index 1 when iterating an array backwards?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
How widely used is the term Treppenwitz? Is it something that most Germans know?
Regex in IF condition in awk
macOS-like app switching in Plasma 5
Letter Boxed validator
How to pronounce "criar"?
How can players work together to take actions that are otherwise impossible?
Create Pages from Database
Can a non-EU citizen traveling with me come with me through the EU passport line?
What are the pros and cons of Aerospike nosecones?
Black holes as heat sinks
When is phishing education going too far?
List *all* the tuples!
Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?
How can I make names more distinctive without making them longer?
Should I call the interviewer directly, if HR aren't responding?
What is a quick way to find the reverse complement in bash
What is the longest distance a 13th level monk can jump while attacking on the same turn?
Date formating in QGIS expression
How can whole tone melodies sound more interesting?
Is there a concise way to say "all of the X, one of each"?
How to say 'striped' in Latin
Regex in IF condition in awk
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 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
4 hours ago
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♦
4 hours ago
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 3 hours ago
muru
37.9k590166
37.9k590166
asked 4 hours ago
LaxmanLaxman
254
254
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
4 hours ago
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♦
4 hours ago
add a comment |
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
4 hours ago
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♦
4 hours ago
1
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
4 hours ago
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
4 hours ago
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♦
4 hours ago
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♦
4 hours ago
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
4 hours ago
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
4 hours ago
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
4 hours ago
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 >.
answered 4 hours ago
terdon♦terdon
134k33270450
134k33270450
Yes. I was using=~format.
– Laxman
4 hours ago
add a comment |
Yes. I was using=~format.
– Laxman
4 hours ago
Yes. I was using
=~ format.– Laxman
4 hours ago
Yes. I was using
=~ format.– Laxman
4 hours ago
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
4 hours ago
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♦
4 hours ago