Sort a text file by line length including spaces

后端 未结 11 2136
故里飘歌
故里飘歌 2020-11-27 11:21

I have a CSV file that looks like this

AS2345,ASDF1232, Mr. Plain Example, 110 Binary ave.,Atlantis,RI,12345,(999)123-5555,1.56
AS2345,ASDF1232, Mrs. Plain Exampl         


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 11:35

    using Raku (formerly known as Perl6)

    ~$ cat "BinaryAve.txt" | raku -e 'given lines() {.sort(*.chars).join("\n").say};'
    
    AS2345,ASDF1232, Mr. Plain Example, 110 Binary ave.,Atlantis,RI,12345,(999)123-5555,1.56
    AS2345,ASDF1232, Mr. Plain Example, 110 Ternary ave.,Some City,RI,12345,(999)123-5555,1.56
    AS2345,ASDF1232, Mr. Plain Example, 110 Binary ave.,Liberty City,RI,12345,(999)123-5555,1.56
    AS2345,ASDF1232, Mrs. Plain Example, 1121110 Ternary st.                                        110 Binary ave..,Atlantis,RI,12345,(999)123-5555,1.56
    

    To reverse the sort, add .reverse in the middle of the chain of method calls--immediately after .sort(). Here's code showing that .chars includes spaces:

    ~$ cat "number_triangle.txt" | raku -e 'given lines() {.map(*.chars).say};'
    (1 3 5 7 9 11 13 15 17 19 0)
    ~$ cat "number_triangle.txt"
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 0
    

    Here's a time comparison between awk and Raku using a 9.1MB txt file from Genbank:

    ~$ time cat "rat_whole_genome.txt" | raku -e 'given lines() {.sort(*.chars).join("\n").say};' > /dev/null
        
        real    0m1.308s
        user    0m1.213s
        sys 0m0.173s
        
    ~$ #awk code from neillb
    ~$ time cat "rat_whole_genome.txt" | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2-  > /dev/null
        
        real    0m1.189s
        user    0m1.170s
        sys 0m0.050s
    

    HTH.

    https://raku.org

提交回复
热议问题