On 12/6/2007 1:40 PM, aitatanit@[EMAIL PROTECTED]
wrote:
> Hi all,
>
> This series of awk commands work great on the command line:
> $ awk -F\; '/RAWIMUSA/{print $2}' 1.txt | awk -F\* '{print $1}' | awk -
> F, '{print $1 "," $2 "," substr($3, 7,1) "," $4 "," $5 "," $6 ","
> $7 "," $8 "," $9}' > out
>
>
> However, when I put this in a script, I get the following error:
>
> $ awk: 1: unexpected character '.'
>
>
> (I removed the awk words from the above command)
You're confused about the difference between an awk script and a shell
script.
What you have above is a shell script that invokes 3 awk scripts via a
series of
pipes. The shell script is:
$ awk -F\; 's1' 1.txt | awk -F\* 's2' | awk -F, 's3' > out
While the 3 awk scripts are:
s1:
/RAWIMUSA/{print $2}
s2:
{print $1}
s3:
{print $1 "," $2 "," substr($3, 7,1) "," $4 "," $5 "," $6 "," $7
"," $8
"," $9}
> I removed the file 1.txt from inside the script and tried to redirect
> it's output to the script by using the cat command by I got the same
> error.
That would be pointless - all it'd do is have the shell open the file
instead of
awk, introduce yet another pipe, and make your awk scrip unable to set the
FILENAME variable.
> Can you please let me know what I am doing wrong and what I
> need to do to fix it?
If you want to have a shell script, just put your command line in a file
and
execute that. If you want to convert the above to an awk script and invoke
it
using the awk command, put this in a file called, say, xyz.awk:
BEGIN{FS=";"; OFS=","}
/RAWIMUSA/{ sub(/\*.*/,"",$2); split($2,a,OFS);
print a[1],a[2],substr(a[3],7,1),a[4],a[5],a[6],a[7],a[8],a[9] }
and invoke it as:
awk -f xyz.awk 1.txt > out
There may be a better way to do what you want - I'm just translating your
scripts as-is.
Ed.


|