Rczone
Rczone

Reputation: 501

Rearrange rows in a file based on the column

I'm looking to rearrange the rows in this text file(test.txt) based on the last column which are having some numbers.For example in the below file we see numbers 714,708,816,202,222 respectively for last column.so now i want to rearrange entire column in ascending order based on the last column

wwwdev.comp.Hello:443          Hello, Inc.     Valid    May 20 2018 714 
Hostname                          cert          expiry
hidev.comp.Hello:443           Hello, Inc.     Valid    May 14 2018 708 
 hidev.comp.Hello:443           Hello, Inc.     Valid   --------------                          
gtdev.Hello:443                Hello, Inc.     Valid    Aug 30 2018 816                                                                                       
wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 202                                
wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 222 

Expected O/P:

   wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 202 
   Hostname                          cert          expiry
    wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 222 
    hidev.comp.Hello:443           Hello, Inc.     Valid   --------------  
    hidev.comp.Hello:443           Hello, Inc.     Valid    May 14 2018 708
    wwwdev.comp.Hello:443          Hello, Inc.     Valid    May 20 2018 714
    gtdev.Hello:443                Hello, Inc.     Valid    Aug 30 2018 816 

Tried:

 sort -k6 -n test.txt

Please suggest.

Note:i have to skip the rows that are without numbers and continue with next row.

Upvotes: 0

Views: 143

Answers (2)

Ed Morton
Ed Morton

Reputation: 204154

With GNU awk 4.* for sorted_in:

$ cat tst.awk
/[0-9]+\s*$/ { sorted[$NF]=$0; next }
{ fixed[NR]=$0 }
END {
    PROCINFO["sorted_in"] = "@ind_num_asc"
    for (key in sorted) {
        if (++onr in fixed) {
            print fixed[onr++]
        }
        print sorted[key]
    }
}

$ awk -f tst.awk file
wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 202
Hostname                          cert          expiry
wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 222
 hidev.comp.Hello:443           Hello, Inc.     Valid   --------------
hidev.comp.Hello:443           Hello, Inc.     Valid    May 14 2018 708
wwwdev.comp.Hello:443          Hello, Inc.     Valid    May 20 2018 714
gtdev.Hello:443                Hello, Inc.     Valid    Aug 30 2018 816

Upvotes: 0

karakfa
karakfa

Reputation: 67507

$ sort -n -k8 file

wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 202
wwwdev.Hello:443               Hello, Inc.     Valid    Dec 24 2016 222
hidev.comp.Hello:443           Hello, Inc.     Valid    May 14 2018 708
wwwdev.comp.Hello:443          Hello, Inc.     Valid    May 20 2018 714
gtdev.Hello:443                Hello, Inc.     Valid    Aug 30 2018 816  

if you don't know how many columns there are, better decorate/sort/undecorate with awk.

$ awk '{print $NF "\t" $0}' file | sort -n -k1,1 | cut -f2-

Upvotes: 4

Related Questions