Find the answer to your Linux question:
Results 1 to 2 of 2
So I have a text file that is very hard to sort. I've tried everything in the sort command to get it in order. Here's some of the file: Capture.jpg...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    5

    Sorting text file


    So I have a text file that is very hard to sort. I've tried everything in the sort command to get it in order. Here's some of the file:

    Capture.jpg

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I assume you want to sort by the value of the integers in the first column (separated by colons), less the RP? If so, then try this bash code:

    Code:
    #!/bin/bash
    declare -a array
    declare -i i
    
    # iterate over each line in text file
    while read line; do
    
      # get the index number from 1st column on left
      i=$(echo $line|awk -F: '{print $1}'|sed -e 's|^RP||')
    
      # save to array
      array[$i]=$line
    
      # save index to variable
      [ -z "$num" ] && num=$i || num="$num $i"
    done < <(cat 'sort.txt')
    
    # sort the index numbers in the saved variable numerically
    sorted=$(printf "$num"|sed -e 's| |\n|g'|sort -n)
    
    # loop thru array in sorted order and print values
    for s in $sorted; do
      echo ${array[$s]}
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •