Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-03-2011, 05:33 AM   #1
LQ Newbie
Registered: Mar 2011
Location: Keerbergen, Belgium
Posts: 3

Rep: Reputation: 0
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,
Old 03-03-2011, 06:01 AM   #2
LQ Newbie
Registered: Apr 2007
Location: Ontario
Posts: 6

Rep: Reputation: 2
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.
1 members found this post helpful.
Old 03-03-2011, 06:14 AM   #3
LQ Newbie
Registered: Mar 2011
Location: Keerbergen, Belgium
Posts: 3

Original Poster
Rep: Reputation: 0
I am using WinVi as editor.

I'll try nano as editor.

Old 03-03-2011, 06:25 AM   #4
LQ Newbie
Registered: Mar 2011
Location: Keerbergen, Belgium
Posts: 3

Original Poster
Rep: Reputation: 0
OK, using nano as editor is doing the job.

Old 03-03-2011, 08:07 AM   #5
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,303

Rep: Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249Reputation: 2249
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP script runs from the command line...but not on Apache custangro Linux - Enterprise 3 07-02-2010 07:38 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 06:25 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
Script runs fine from command line, but not when attempted from Cron hwest Linux - Newbie 8 12-11-2008 07:48 PM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 05:14 PM

All times are GMT -5. The time now is 05:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration