LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   AWK runs in command line, but not in script (http://www.linuxquestions.org/questions/linux-newbie-8/awk-runs-in-command-line-but-not-in-script-866189/)

PaulVanLil 03-03-2011 04:33 AM

AWK runs in command line, but not in script
 
Hi,

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,
Code:

awk --version
gives the following output:

Code:

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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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
Code:

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

Code:

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:
Code:

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
Code:

awk -f advice.awk
the result is:
Code:

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,
Paul

ryoken31 03-03-2011 05: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 05:14 AM

I am using WinVi as editor.

I'll try nano as editor.

Thanks,
Paul

PaulVanLil 03-03-2011 05:25 AM

OK, using nano as editor is doing the job.

Thanks,
Paul

grail 03-03-2011 07: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:
Code:

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


All times are GMT -5. The time now is 11:17 PM.