Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Several Pip...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 5 Topic 2094 of 2376
Post > Topic >>

Re: Several Pipes inside an awk script

by Ed Morton <morton@[EMAIL PROTECTED] > Dec 6, 2007 at 03:38 PM

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.
 




 5 Posts in Topic:
Several Pipes inside an awk script
aitatanit@[EMAIL PROTECTE  2007-12-06 11:40:53 
Re: Several Pipes inside an awk script
Cesar Rabak <csrabak@[  2007-12-06 18:37:41 
Re: Several Pipes inside an awk script
aitatanit@[EMAIL PROTECTE  2007-12-06 12:50:06 
Re: Several Pipes inside an awk script
Ed Morton <morton@[EMA  2007-12-06 15:38:31 
Re: Several Pipes inside an awk script
aitatanit@[EMAIL PROTECTE  2007-12-06 13:50:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 8:01:46 CDT 2008.