Pergunta

I have a DCL script that creates a .txt file that looks something like this

something,somethingelse,00000004
somethingdifferent,somethingelse1,00000002
anotherline,line,00000015

I need to sort the file by the 3rd column highest to lowest ex:

anotherline,line,00000015
something,somethingelse,00000004
somethingdifferent,somethingelse1,00000002

Is it best to use the sort command, if so everything i've seen required a position number, how can this be done if each line would have a different start position?

If sort is a bad way to handle this is there something else or can I somehow handle this while writing the lines to the file.

I've only been working with VMS/DCL for a few weeks now so i'm not fimilar with all of the commands yet.

Thanks!

Foi útil?

Solução

As you already noticed, the VMS sort expects fields with a fixed start position within a record. You can not specify a field by a separator. If you want to use the VMS sort you have to make sure your third field starts at the same column, for all records. In other words, you have to pad preceding fields. If you have control on how the file is created, this may work for you. If you don't or you don't know how big the string in front of the sort field will be, this may not be a workaround. Maybe changing the order of the fields is an option.

On the other hand, you may find GNV installed on your system. Then you can try to use its sort, which is a GNU style sort. That is, $ mcr gnv$gnu:[bin]sort -t, -k3 -r x.txt may get you the wanted results.

Outras dicas

VMS Sort is indeed not really equipped for this. Reformatting as you did is about the only way.

If you so not have access to GNV sort on the OpenVMS system then perhaps you have, or can install PERL? Is is somewhat easier to install.

In perl there are of course many ways. For example using an anonymous sort function ( $a is first arg, $b second; <> reads all input )

$ perl -e "print sort { 0+(split /,/,$b)[1] <=>  0+(split /,/,$a)[2]} <>" x.x

where the 0 + forces numeric evaluation. For (fixed length?) string compare use:

$ perl -e "print sort { (split /,/,$b)[2] cmp (split /,/,$a)[2]} <>" x.x

hth, Hein.enter code here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top