yamusinx
yamusinx

Reputation: 1

How to sort number in file (highest)

There is a file included many information. I want to sort several sentences with included numbers.

In Files, there are several sentences.

There are 7 lines below (middle line is blank)

GRELUP.C.3a.or:ndiff_c_fail_a_same_well = SELECT -inside GRELUP.C.3a.or:ndiff_c_fail_a GRELUP.C.3a.or:_EPTMPL312066 -not
generate layer GRELUP.C.3a.or:ndiff_c_fail_a_same_well, TYP = P, HPN = 0, FPN = 0, HEN = 0, FEN = 0
Time: cpu=0.00/8818.64  real=0.30/1875.23  Memory: 160.81/245.20/245.20

GRELUP.C.3a.or:ndiff_c_fail_a = SELECT -inside GRELUP.C.3a.or:ndiff_c_eg GRELUP.C.3a.or:well_cont_a_sized_a -not
generate layer GRELUP.C.3a.or:ndiff_c_fail_a, TYP = P, HPN = 0, FPN = 0, HEN = 0, FEN = 0
Time: cpu=0.00/8818.64  real=1.10/1875.23  Memory: 180.84/252.29/252.29

What I want to return line are below.

GRELUP.C.3a.or:ndiff_c_fail_a real=1.10/1875.23
GRELUP.C.3a.or:ndiff_c_fail_a_same_well real=0.30/1875.23

In other words, high number behind of "real=" is sorted first and added specific words behind "generate layer" above line.

Upvotes: 0

Views: 58

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

I suggest doing this in several stages, as it is so much easier to take things in limited steps:

  1. Split the data into records.
  2. Convert each record into a reduced form that just has the information you want.
  3. Sort now that you can easily determine what to sort by.
  4. (Optional, depending on how you do step 2) Extract the information to print.

If your data is small enough to fit into memory, the first step can be done with:

proc splitIntoRecords {data} {
    # U+001E is the official ASCII record separator; it's not used much!
    regsub -all {\n{2,}} $data \u001e data
    return [split $data \u001e]
}

I'm not quite so sure about the conversion step; this might work (on a single record; I'll lift to the collection with lmap later):

proc convertRecord {record} {
    # We extract the parts we want to print and the part we want to sort by
    regexp {(^\S+).*(real=[^\s/]+/(\S+))} $record -> name time val
    return [list "$name $time" $val]
}

Once that's done, we can lsort -real -decreasing with a -index specified to get the collation key (the $vals we extracted above), and printing is now trivial:

set records [lmap r [splitIntoRecords $data] {convertRecord $r}]
foreach r [lsort -real -decreasing -index 1 $records] {
    puts [lindex $r 0]
}

Upvotes: 1

Related Questions