[SOLVED]Using AWK with Regular Expressions

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
shadowfeind
Level 1
Level 1
Posts: 36
Joined: Thu Sep 24, 2020 2:05 pm

[SOLVED]Using AWK with Regular Expressions

Post by shadowfeind »

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
Last edited by shadowfeind on Fri May 07, 2021 6:41 am, edited 3 times in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 25443
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Using AWK with Regular Expressions

Post by xenopeek »

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
}
Image
rene
Level 17
Level 17
Posts: 7517
Joined: Sun Mar 27, 2016 6:58 pm

Re: Using AWK with Regular Expressions

Post by rene »

The original issue is simply that an awk script is (basically) a series of PATTERN { ACTION } statements. One such pair is in this case for example

Code: Select all

BEGIN { FS="," }
where the special pattern BEGIN matches at the start of execution. It is then attempted to have a pattern-action of

Code: Select all

$2 ~ /King/ { print $1 ",", $6 }
which says that if the second field of a line regex-matches /King/ do the corresponding action { ... }. What is however in fact said is

Code: Select all

{
$2 ~ /King/ { print $1 ",", $6 }
}
which is to say

Code: Select all

{ $2 ~ /King/ { print $1 ",", $6 } }
hence an empty pattern (meaning it matches for every line) and then an ACTION starting with $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 }
rene
Level 17
Level 17
Posts: 7517
Joined: Sun Mar 27, 2016 6:58 pm

Re: Using AWK with Regular Expressions

Post by rene »

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.
shadowfeind
Level 1
Level 1
Posts: 36
Joined: Thu Sep 24, 2020 2:05 pm

Re: Using AWK with Regular Expressions

Post by shadowfeind »

@rene your explanation makes perfect sense. I mark this solved
rene
Level 17
Level 17
Posts: 7517
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED]Using AWK with Regular Expressions

Post by rene »

Damnit. I was using you to finally get this subforum moved. Fat chance now.... ;-)
Post Reply