Rearranging fields from a pipe

I have a large file that I am pulling only certain fields out of but my output I would like to rearrange the field order via a pipe. I have been looking through the site and man pages and have come to a loss.

I am running on HP

cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> > <outfile>

I would like to have field 4 moved to be the first field.

I tried something like using awk but it is not intuitive

cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> | awk '(printf("-%2s-%3s-%3s-%2s\n", $4,$1,$2,$3))' > <outfile>

thanks

Some corrections

cut -c33-38,44-46,62-65,91-98 <file> | grep -e <value> | 
        awk ' {
                  printf("-%8s-%6s-%3s-%4s\n", $4,$1,$2,$3)
               } ' > <outfile>

Assuming your cut options got the right fields

Perhaps just use awk like this...

awk '/value/ {print substr($0,91,8) substr($0,33,6) etc}' infile > outfile

I was having the same problem and ended up using the paste command to rearrange:

cut -d , -f 1 some.csv > 1.csv
cut -d , -f 10 some.csv > 10.csv
paste -d ',' 10.csv 1.csv > combined.csv
rm 1.csv
rm 10.csv

1 and 10 are the column(field) numbers(position)

cut ..... <file> | grep ... | while read field_1 field_2 field_3 field_4 ; do
     print - "$field_4 $field_1 $field_2 $field_3"
done > <outfile>

Of course awk could do exactly the same but using a lot more system resources in the process hence justifying the procurement of new hardware more early and hence is the preferable solution.

Always strife to use the wait()-function to its full potential. ;-))

Btw.: you haven't asked, but I suppose your data are in tabular form and you are 'cut'-ting the field boundaries. There is an easier way to do that if the field entries contain no blanks:

sed 's/<space><space>*/<space>/g' <file> | cut -d'<space>' -f<1-4>

bakunin