Hello friends, I am trying to learn awk programming and am running into some syntax error issue.
I am trying to write a file to use with awk -f filename .
The input file to run through awk has the following structure:
Bat Man,King Inc.,123 4th Ave.,Boston,MA 01234,678-0999
Super Man,Kong Corp.,56 Test Drive,Amesbury,MA 01234,678-0999
The awk file i have has the following contents:
BEGIN {FS=","}
{
$2 ~ /King/ {print $1 ",", $6}
}
when i run this using awk -f awkscript.awk input.txt i get the following error:
awk: awkscript.awk:3: $2 ~ /King/ {print $1 ",", $6}
awk: awkscript.awk:3: ^ syntax error
It runs correctly if I use this: awk -F"," '$2 ~ /King/ {print $1 ",", $6}' input.txt
This is just a small test. I am actually trying to write a file with multiple test conditions in it and I am not sure what the correct syntax for it would be.
Thank you
[SOLVED]Using AWK with Regular Expressions
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
-
- Level 1
- Posts: 36
- Joined: Thu Sep 24, 2020 2:05 pm
[SOLVED]Using AWK with Regular Expressions
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Using AWK with Regular Expressions
I don't know enough awk to tell you why it works from the command line and not from the script. But changing the script to this makes it also work:
Code: Select all
BEGIN {FS=","}
{
if ($2 ~ /King/) print $1 ",", $6
}
Re: Using AWK with Regular Expressions
The original issue is simply that an awk script is (basically) a series of
where the special pattern BEGIN matches at the start of execution. It is then attempted to have a pattern-action of
which says that if the second field of a line regex-matches /King/ do the corresponding action
which is to say
hence an empty pattern (meaning it matches for every line) and then an ACTION starting with
PATTERN { ACTION }
statements. One such pair is in this case for exampleCode: Select all
BEGIN { FS="," }
Code: Select all
$2 ~ /King/ { print $1 ",", $6 }
{ ... }
. What is however in fact said isCode: Select all
{
$2 ~ /King/ { print $1 ",", $6 }
}
Code: Select all
{ $2 ~ /King/ { print $1 ",", $6 } }
$2 ~ /King
which as an action is indeed a syntax error. As such xenopeek's adjustment works by him turning the action into a proper one, but what OP was aiming for is simply:Code: Select all
BEGIN { FS="," }
$2 ~ /King/ { print $1 ",", $6 }
Re: Using AWK with Regular Expressions
This specific subforum has a below-average follow-up rate, and I expect this to be due to poster not always even noticing that a post has been moved to it. Think it's overdue to rename this e.g. Scripting & Programming and move it out of the "Personalize and Customize Linux Mint" section so as to not give the impression that this is/can be only about Linux Mint as such. This then in order for posters to more easily find this section themselves.
-
- Level 1
- Posts: 36
- Joined: Thu Sep 24, 2020 2:05 pm
Re: Using AWK with Regular Expressions
@rene your explanation makes perfect sense. I mark this solved
Re: [SOLVED]Using AWK with Regular Expressions
Damnit. I was using you to finally get this subforum moved. Fat chance now....