[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
New built-in variable
From: |
Jean-Michel ELYN |
Subject: |
New built-in variable |
Date: |
Mon, 15 Dec 2008 09:51:30 +0100 |
User-agent: |
Thunderbird 2.0.0.16 (X11/20080723) |
Hello,
It seems previous mail has arrived empty. Going to try again...
I often use AWK (actually GAWK on Linux) because it is really powerful.
However, I have a simple problem that is not solve in an elegant way, in
my point of view.
*Problem:*
I want to sort numerical values on each line of a file.
file_in:
3 55 12 4
2 5 0
file_out:
3 4 12 55
0 2 5
*Current solution:*
gawk '{split($0, line); asort(line); for(i = 1; i <= NF; i++) {$i =
line[i]}; print}' file_in > file_out
*Difficulty:*
As it is not possible to access current record ($0) as a standard array
variable, I need to split input ($0) again into an array variable, asort
it, then assign it back to input (field by field!) and finally print it.
*Possible solution:*
Create a built-in array variable named FIELDS (for example) that
actually refers to $1, $2, .... The solution would become:
gawk '{asort(FIELDS); print}' file_in > file_out
This new built-in variable would much improve all work on small changes
such as field order.