Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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:


  2. #2
    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:

    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
      # 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]}

Posting Permissions

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