Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash sort by regexp

I have something about 100 files with the following syntax

ahfsdjfhdfhj_EPI_34_fdsafasdf
asdfasdf_EPI_2_fdsf
hfdjh_EPI_8_dhfffffffffff
ffffffffffasdfsdf_EPI_1_fyyy44

...

There is always EPI_NUMBER. How can I sort it by this number?

like image 570
mecio Avatar asked Dec 30 '11 14:12

mecio


3 Answers

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/'
like image 142
anubhava Avatar answered Oct 24 '22 06:10

anubhava


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

like image 4
codaddict Avatar answered Oct 24 '22 08:10

codaddict


 sed -e 's/EPI_/EPI /' file1 file2 ...|sort -n -k 2 -t ' '

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

like image 3
holygeek Avatar answered Oct 24 '22 06:10

holygeek