Bash sort by regexp

后端 未结 4 401
自闭症患者
自闭症患者 2020-12-31 15:45

I have something about 100 files with the following syntax

ahfsdjfhdfhj_EPI_34_fdsafasdf
asdfasdf_EPI_2_fdsf
hfdjh_EPI_8_dhfffffffffff
ffffffffffasdfsdf_EPI_         


        
相关标签:
4条回答
  • 2020-12-31 16:06

    If Perl is okay you can:

    print sort foo <>;    
    sub foo {
            ($x = $a) =~s/.*EPI_(\d+).*/$1/;
            ($y = $b) =~s/.*EPI_(\d+).*/$1/;
            return $x <=> $y;
    }
    

    and use it as:

    perl prg.pl inputfile
    

    See it

    0 讨论(0)
  • 2020-12-31 16:06

    This might work for you:

     ls | sed 's/.*EPI_\([0-9]*\)/\1 &/' | sort -n | sed 's/\S* //'
    
    0 讨论(0)
  • 2020-12-31 16:07

    From your example it appears that delimiter is _ and text EPI_nnn comes at the same position after delimiter _. If that is always the case then you can use following command to sort the file:

    sort -n -t "_" -k 3 file.txt
    

    UPDATE:

    If position of EPI_ text is not fixed then use following shell command:

    sed 's/^\(.*EPI_\)\(.*\)$/\2##\1/' file.txt | sort -n -t "_" -k1 | sed 's/^\(.*\)##\(.*\)$/\2\1/'
    
    0 讨论(0)
  • 2020-12-31 16:11
     sed -e 's/EPI_/EPI /' file1 file2 ...|sort -n -k 2 -t ' '
    

    Pipe that to sed -e 's/ /_/' to get back the original form.

    0 讨论(0)
提交回复
热议问题