Issues with AWK

Hi there
I have written a ksh script on a Red Hat OS and the following extract works.
awk '{if (NR != 1) {print $rec1_field }}' $file1 >> combined

When I run the same script on an AIX OS, I get the following error.

awk: 0602-562 Field $() is not correct.
The input line number is 2. The file is LetterWriting_Type_EPRO.csv.
The source line number is 1.

It would appear that I need to assign the script variable $rec1_field.

Can anyone advise

Thanks
Alan

$rec1_field is not an awk variable. to pass shell variable to awk, use -v option

Thanks for your prompt reply.

Your advice has fix one problem, but raised another. The variable $rec1_field, actually contains $1,$2,$3 etc, in other words it contains the format required to produce a desired result. When I use the -v option
awk -v awkvar=$new_rec1_field '{if ( NR != 1 ) { print $awkvar }}' $file1 >> combined

awk displays and error stating it can't parse $($1,$2,$3 etc

Any more switches you can turn on for me.

Thanks

Alan

print awkvar, not $awkvar

Sorry I tried that.

Instead of getting the field information printed, I get $1,$2,$3..... etc printed. That is variable $1 is not being replaced by the first field in the file $file1

What is your exact requirement? What does the variable new_rec1_field hold? If it is refering to fields -$1, $2 etc. why can't it be directly use inside awk?

cheers,
Devaraj Takhellambam

I have written a script that combines several files that contains both similar and dissimilar fields within each record. The resultant file needs to be a concatenation of all files vertically, not horizontally (ie can't use cat). Can't use join as any common field can only be displayed once in the resultant file.
So I created a variable "$rec1_field" that contains the order of the variables I need to be output for each record, as parsed by awk.
The variable literally contains "$1,$2,$3,....". What I thought I could do ,is replace {print $1} within awk with {print $rec1_field}, and let awk expand the variable to $1,$2,etc.

I developed the script on a Linus Red Hat platform and it works 100%, but when I run it under AIX it fails. See previous posts.

The hard way :

awk -v awkvar=$new_rec1_field 'NR>1{y=system(echo "$new_rec1_field");print $y}'  $file1 > combined

cheers,
Devaraj Takhellambam

Supposing the variable $rec1_field contains the field numbers delimited by a space as follow: "3 5 4 6 7":

awk -v var="$rec1_field" 'BEGIN{n=split(var,a)} 
NR > 1 {
  for(i=1;i<=n;i++) {
    printf("%s%c",$a,i==n?"\n":" ")
  }
}' file

devtakh,

Have you tried your solution?
The system command returns the exit code of the echo command, not the content of the variable.

You are right Franklin. This will return the exit code..

Sorry guys. It was well past my bed time, so missed your replies.

I will give your suggestions a go and advise

Thanks very much for all your input.

Alan

Guys

Thanks for your help.

The script Franklin52 supplied, worked like a charm.

Once again, thanks.