2013-06-05 18 views
5

Benbash sıralama listesi

/this/is/a/file/path/product-2.0/file/name/7 
/this/is/a/file/path/product-2.0/file/name/10 
/this/is/a/file/path/product-2.0/file/name/12 
/this/is/a/file/path/product-2.0/file/name/13 
/this/is/a/file/path/product-2.0/file/name/6 
/this/is/a/file/path/product-2.0/file/name/8 
/this/is/a/file/path/product-2.0/file/name/9 

zaman grep aracılığıyla Ben boru onu bu bir çeşit şöyle sıralamak ihtiyacım sonunda bir sürüm numarasına sahip dosyaların bir listesi var:

echo $files | sort -n 
/this/is/a/file/path/product-2.0/file/name/10 
/this/is/a/file/path/product-2.0/file/name/12 
/this/is/a/file/path/product-2.0/file/name/13 
/this/is/a/file/path/product-2.0/file/name/6 
/this/is/a/file/path/product-2.0/file/name/7 
/this/is/a/file/path/product-2.0/file/name/8 
/this/is/a/file/path/product-2.0/file/name/9 

-n dosya ismindeki ilk numara ile -n karıştırıldığını düşünüyorum.

Geçen sayısına göre sayısal olarak sıralayabilir nasıl

+0

yolun aynı derinlik? – Kevin

+0

evet, ama ben derinlik bilmiyorum –

cevap

11
Kaizen ~/so_test $ cat ztestfile1 | sort -t'/' -n -k10 
/this/is/a/file/path/product-2.0/file/name/6 
/this/is/a/file/path/product-2.0/file/name/7 
/this/is/a/file/path/product-2.0/file/name/8 
/this/is/a/file/path/product-2.0/file/name/9 
/this/is/a/file/path/product-2.0/file/name/10 
/this/is/a/file/path/product-2.0/file/name/12 
/this/is/a/file/path/product-2.0/file/name/13 

kullanan bir örnek şu yardımcı olmaktadır? yani

alternatif bir yol

Kaizen ~/so_test $ cat ztestfile1 | sort -V 
/this/is/a/file/path/product-2.0/file/name/6 
/this/is/a/file/path/product-2.0/file/name/7 
/this/is/a/file/path/product-2.0/file/name/8 
/this/is/a/file/path/product-2.0/file/name/9 
/this/is/a/file/path/product-2.0/file/name/10 
/this/is/a/file/path/product-2.0/file/name/12 
/this/is/a/file/path/product-2.0/file/name/13 

its sıralamak için bir dizede sayısal farklılıkları içine bakar tür bir -V (Sermaye) seçeneğini unutmayın ..... pozisyondan bağımsız olmak ... .. sürüm numarası gibi.

adam sayfa metin ::

-V, --version-sort 
      natural sort of (version) numbers within text 
+0

Bu benim denemekten başka bir şey deniyordum. man sayfa ve ekstra boşluk vardı 'sort -n -t '/' -k 10' ve görünüşe göre sıralama bu gibi değil –

+0

ohh tamam, sorun şimdi sıralanır görünüyor ... :) – nsd

+0

Bu tür ile dikkat edin Çözüm bilmek ve dosyaların derinliğini belirtmek zorunda. – michas

3

Bir sütun ayırıcı olarak / kullanabilirsiniz: 1 alana göre, ön sort son alanı getirmek

sort -n -t/ -k 10 
+0

anlamaya çalışıyorum .... cevabınız 1 dk bizden önceydi :) +1 – nsd

1

Kullanım awk Daha sonra ilk alandan kurtulmak için cut uygulayın. Bir here document

altına
awk -F'/' '{print($NF"/"$0)}' <<! | sort -k1n,1n -t'/' | cut -f2- -d'/' 
> /this/is/a/file/path/product-2.0/file/name/7 
> /this/is/a/file/path/product-2.0/file/name/10 
> /this/is/a/file/path/product-2.0/file/name/12 
> /this/is/a/file/path/product-2.0/file/name/13 
> /this/is/a/file/path/product-2.0/file/name/6 
> /this/is/a/file/path/product-2.0/file/name/8 
> /this/is/a/file/path/product-2.0/file/name/9 
> ! 
/this/is/a/file/path/product-2.0/file/name/6 
/this/is/a/file/path/product-2.0/file/name/7 
/this/is/a/file/path/product-2.0/file/name/8 
/this/is/a/file/path/product-2.0/file/name/9 
/this/is/a/file/path/product-2.0/file/name/10 
/this/is/a/file/path/product-2.0/file/name/12 
/this/is/a/file/path/product-2.0/file/name/13 
0

Bu sıralama, son değerini alarak ve hattın başında yerleştirerek, saha ayırıcı olarak / kullanarak yoldaki son öğeye göre dizildi ve sonra değeri satırın başlangıcından kaldırır.

cat data | awk 'BEGIN { FS="/"} { print $(NF) " " $0 }' | sort -n | sed 's/^.*\s//g' 
0

Ya Perl veya yakut gibi tek satırlık bir şey kullanın: Her zaman

ruby -we 'print *readlines.sort_by{|x| File.basename(x).to_i}'