How to sort a column of a file without ordering the rows?

Asked

Viewed 472 times

2

I have a file that has three columns, and several rows. The second column is composed of numerical values, and I want to reorganize it, in ascending order, but without affecting the previous column and the subsequent one.

exemplo do arquivo:

7.31937  736    /tmp/ref13
7.3223   5373   /tmp/ref13
7.32816  768    /tmp/ref13
7.32955  5370   /tmp/ref10

o que eu quero:


7.31937  736    /tmp/ref13
7.3223   768    /tmp/ref13
7.32816  5370   /tmp/ref13
7.32955  5373   /tmp/ref10

I tried to sort -k2 -n, but this command is moving the other columns =(

Will someone help me? Thanks!

Edit:

[Resolvido]Comando utilizado:

`paste -d' '  <(awk '{print $1}' nome_arquivo) <(awk '{print $2}' nome_arquivo | sort -n) <(awk '{print $3}' nome_arquivo)`
  • Make a tour by stackoverflow, learn a little about the rules and good practices. Then edit your question and enter the code of what you’ve tried...

  • Yes, it’s a good question now.. It’s clearer and easier to understand

1 answer

4


Script:

#!/bin/bash

INPUT=$1
OUTPUT=$2

awk '{ print $1 }' $INPUT           > $$.1
awk '{ print $2 }' $INPUT | sort -n > $$.2
awk '{ print $2 }' $INPUT           > $$.3

paste $$.1 $$.2 $$.3 > $OUTPUT
rm $$1 $$.2 $$.3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.