Bash sort by regexp

后端 未结 4 406
自闭症患者
自闭症患者 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: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/'
    

提交回复
热议问题