Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

re-number - awk?

38 views
Skip to first unread message

john.la...@gmail.com

unread,
Apr 21, 2017, 4:01:05 PM4/21/17
to
I have some files where I would like to re-number a column as follows: in the input column 5 is numbered from 1-4 and is sorted descending and has duplicate values. I want to reverse the order of the values, have them start from 1, and in cases of ties give them the same value - as in this example:

Input:
NDH 1 3 4 4
NDH 1 2 4 4
NDH 1 1 4 4
NDH 1 5 1 3
NDH 1 4 4 1

Desired output:
NDH 1 3 4 1
NDH 1 2 4 1
NDH 1 1 4 1
NDH 1 5 1 2
NDH 1 4 4 3

Any ideas how to do that?

-- John Larson

Ed Morton

unread,
Apr 21, 2017, 4:08:11 PM4/21/17
to
Assuming when you say "in case of ties" you mean in case of the same value in
the 4th column:

$ awk '$4!=p{c++} {$5=c; p=$4} 1' file
NDH 1 3 4 1
NDH 1 2 4 1
NDH 1 1 4 1
NDH 1 5 1 2
NDH 1 4 4 3

If that's not what you mean then please clarify.

Ed.

Ed Morton

unread,
Apr 21, 2017, 4:10:33 PM4/21/17
to
Or maybe you meant duplicates in the existing 5th column:

$ awk '$5!=p{c++} {p=$5; $5=c} 1' file
NDH 1 3 4 1
NDH 1 2 4 1
NDH 1 1 4 1
NDH 1 5 1 2
NDH 1 4 4 3

Ed.

Wayne

unread,
Apr 21, 2017, 9:39:10 PM4/21/17
to
awk '{ $5 = (5 - $5) } ' file

--
Wayne

john.la...@gmail.com

unread,
Apr 24, 2017, 1:12:36 AM4/24/17
to
On Friday, April 21, 2017 at 10:10:33 PM UTC+2, Ed Morton wrote:
> Or maybe you meant duplicates in the existing 5th column:
>
> $ awk '$5!=p{c++} {p=$5; $5=c} 1' file

Perfect - works like dream! Apologies for not being clear.

-- John
0 new messages