Regex in IF condition in awk
.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
12 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♦
12 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 11 hours ago
muru
37.9k590166
37.9k590166
asked 12 hours ago
LaxmanLaxman
254
254
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
12 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♦
12 hours ago
add a comment |
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
12 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♦
12 hours ago
1
1
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
12 hours ago
See the examples here: gnu.org/software/gawk/manual/gawk.html#Regexp-Usage
– steeldriver
12 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♦
12 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♦
12 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
12 hours ago
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
7 hours ago
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♦
7 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
12 hours ago
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
7 hours ago
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♦
7 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
12 hours ago
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
7 hours ago
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♦
7 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 >.
edited 7 hours ago
answered 12 hours ago
terdon♦terdon
134k33270450
134k33270450
Yes. I was using=~format.
– Laxman
12 hours ago
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
7 hours ago
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♦
7 hours ago
add a comment |
Yes. I was using=~format.
– Laxman
12 hours ago
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
7 hours ago
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♦
7 hours ago
Yes. I was using
=~ format.– Laxman
12 hours ago
Yes. I was using
=~ format.– Laxman
12 hours ago
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
7 hours ago
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
7 hours ago
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♦
7 hours ago
@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♦
7 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
12 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♦
12 hours ago