I needed to sort a text file (using sort) with this format:

[rocha@vorheez tmp]$ head -n 1 li.txt
     ID QUANTITY SIZE PRODUCT DESCRIPTION WITH A LOT OF SPACES I                     TYPE             PRICE   SKU

The important value is at second to last column (PRICE) ’cause I’ll check for products with same price. If prod. description has no spaces, everything will be fine: I just need to sort by 7th column.

I’ve found this reading awk man page:

       The variable NF is set to the total number of fields in the input record.
       References to non-existent fields (i.e. fields after $NF) produce the null-string.  However, assigning  to
       a  non-existent  field  (e.g., $(NF+2) = 5) increases the value of NF

After found how to sort last column with AWK, final script is:

[rocha@vorheez tmp]$ head -n 20 li.txt | awk '{ printf $(NF -1);$(NF - 1) = "" ;printf " "$0"\n" }' | sort -n

Awk made my day.

Posts relacionados:

  1. ActionController::Caching::Pages fail: no cache, no funciona!
  2. SAMBA3: VFS, kernel interface to file systems
  3. Integrate Ruby and Asterisk (call detail record) using ActiveRecord

No comments yet

Deixe Seu Comentário