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;








2















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?










share|improve this question



















  • 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 your awk?

    – terdon
    4 hours ago

















2















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?










share|improve this question



















  • 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 your awk?

    – terdon
    4 hours ago













2












2








2








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 your awk?

    – terdon
    4 hours ago












  • 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 your awk?

    – 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










1 Answer
1






active

oldest

votes


















6














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 >.






share|improve this answer























  • Yes. I was using =~ format.

    – Laxman
    4 hours ago











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
);



);













draft saved

draft discarded


















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









6














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 >.






share|improve this answer























  • Yes. I was using =~ format.

    – Laxman
    4 hours ago















6














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 >.






share|improve this answer























  • Yes. I was using =~ format.

    – Laxman
    4 hours ago













6












6








6







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 >.






share|improve this answer













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 >.







share|improve this answer












share|improve this answer



share|improve this answer










answered 4 hours ago









terdonterdon

134k33270450




134k33270450












  • 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





Yes. I was using =~ format.

– Laxman
4 hours ago

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bett Inhaltsverzeichnis Geschichte | Bettformen | Bettgrößen | Andere Bezeichnungen | Bettenmangel | Betten in der bildenden Kunst | Schlafmedizinische Gesichtspunkte | Siehe auch | Literatur | Weblinks | Einzelnachweise | NavigationsmenüBett, Bettstatt, BettstelleCommons: BettBabybetten: Anwendung, Ausstattungsmerkmale und VergleichskriterienWasserbetten. Vorurteile im TestHapfnNursch10.1007/s11818-012-0584-74006250-8AKS4329276-8

Luksemburg Sisukord Nimi | Asend | Loodus | Riigikord | Haldusjaotus | Rahvastik | Riigikaitse | Majandus | Taristu | Ajalugu | Eesti ja Luksemburgi suhted | Haridus | Kultuur | Vaata ka | Viited | Välislingid | Navigeerimismenüü50° N, 6° EÜlevaade Luksemburgi kaitsealadest.Luksemburgi rahvaarv. Statistikaamet.World Bank'i andmebaasÜlevaade Luksemburgi loodusest.Ülevaade Luksemburgi metsadest.Guy Colling. "Red List of the Vascular Plants of Luxembourg." Travaux scientifiques du Musée national d’histoire naturelle Luxembourg. 2005.Luxembourg’s biodiversity at risk.Maailma kahepaiksete andmebaas.Denis Lepage. "Luxembourg." Avibase.Ülevaade temperatuuridest. Luksemburgi meteoroloogiateenistus.Ülevaade Luksemburgist. Euroopa Liidu esinduse koduleht.Système politique. TerritoireÜlevaade Luksemburgi rahvastikust. Luksemburgi statistikaamet.Luksemburgi rahvastik. Luksemburgi statistikaamet.The World FactbookMonique Borsenberger, Paul Dickes. "Religions au Luxembourg. Quelle évolution entre 1999-2008". Luksemburgi statistikaamet. 2011.Luksemburgi peapiiskopkond. Catholic-Hierarchy.Luksemburgi armee koduleht.Luksemburgi armee relvastus.Eesti Välisministeerium.Luksemburgi rahvastik. Luksemburgi statistikaamet.Luksemburgi Eesti Seltsi koduleht.Helen Eelrand. "Raadio, mis muutis maailma." Eesti Päevaleht. 13. märts 2004.Ülevaade Luksemburgi haridussüsteemist.Ülevaade Luksemburgi keskkoolidest.Luksemburgr

Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6