-   Linux - Newbie (
-   -   AWK runs in command line, but not in script (

PaulVanLil 03-03-2011 05:33 AM

AWK runs in command line, but not in script

I am trying to execute an awk script which is called by a shell script. The result is that it always responds with << ^ invalid char ' ' in expression >>

As I do not have command line access, I use a php script as go-around.

On the command line,

awk --version
gives the following output:


Command: awk --version
GNU Awk 3.1.3
Copyright (C) 1989, 1991-2003 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

So, awk is installed on the server.

On the command line

awk "BEGIN { print \"Don't Panic!\" }"
results in:


Command: awk "BEGIN { print \"Don't Panic!\" }"
Don't Panic!

OK, I do not panic, AWK commands are executed.

Next is to put the awk-command in a script called advice.awk
The commands is:

BEGIN { print "Don't Panic!" }
advice.awk is in the same directory as run.php (which starts the command line) and has chmod 777.

When I give the command

awk -f advice.awk
the result is:

Command: awk -f advice.awk
awk: advice.awk:1: BEGIN { print "Don't Panic!" }
awk: advice.awk:1: ^ invalid char ' ' in expression

Thus, after two days of searching, I start to panic!

What is going wrong?

Thanks in advance,

ryoken31 03-03-2011 06:01 AM

What are you using to create advice.awk? If you're not already, try using vi or nano to create the file. Some text editors add hidden characters at the start/end of text documents that could be causing the issue. I was not able to reproduce the problem on my machine using the same version of awk as you. I only have command line access and was using nano to create the file.

PaulVanLil 03-03-2011 06:14 AM

I am using WinVi as editor.

I'll try nano as editor.


PaulVanLil 03-03-2011 06:25 AM

OK, using nano as editor is doing the job.


grail 03-03-2011 08:07 AM

Just an addon for you, you can list the interpreter at the top of the script the same as you would in say a bash script:

#!/usr/bin/awk -f
Obviously path needs to be where your awk is located :)

All times are GMT -5. The time now is 10:15 AM.