2011-08-04 125 views
0

好的,我被遺棄了。我需要一個shell腳本,它將隨機選擇一個擴展名爲.sct的文件。使用其文件名的一部分選擇六個相關的.mot文件。然後將它們全部移到另一個文件夾。我還需要用戶輸入文件的數量來隨機選擇。選擇隨機文件的擴展名,然後選擇「連接」文件

所以我有一個文件的結構是這樣的:

 
123-12345-00.sct 
123-12345-00.mot 
123-12345-01.mot 
123-12345-02.mot 
123-12345-03.mot 
123-12345-04.mot 
123-12345-05.mot 

123-12346-00.sct 
123-12346-00.mot 
123-12346-01.mot 
123-12346-02.mot 
123-12346-03.mot 
123-12346-04.mot 
123-12346-05.mot 

等。需要隨機選擇文件.sct並將其及其相關文件移動到另一個目錄。希望我已經解釋了這一點。

感謝您的幫助。我可以在VB中做到這一點,但這個UNIX的東西讓我癱瘓了。現在我們通過數千個文件來完成這項工作。

斯科特

+0

也許想法可以在這裏找到:http://stackoverflow.com/questions/414164/how-can-i-select-random-files-from-a-directory-in-bash – Nobody

回答

1

這個腳本的123-12345-*.sct123-12345-*.mot文件移動到一個名爲123-12345等目錄。

注意:這不是隨機選擇一個文件,而是在目錄內的所有文件。您可以修改它以接受隨機文件數量的命令行參數。然後,您需要修改此命令ls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'以使用您的命令行參數,該參數是文件計數並返回隨機數前綴。

將以下內容複製到一個文件中,如mv_sct_mot.sh與您的sct和mot文件位於同一目錄中。

#!/bin/bash 

for prefix in `ls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'`; do 
    mkdir -p ${prefix}; 
    mv ${prefix}-*.{mot,sct} ${prefix}; 
done 

爲了使文件可執行修改它的權限,如:

chmod +x mv_sct_mot.sh 

運行它想:

./mv_sct_mot.sh 
+0

爲什麼你選擇mot文件稍後丟棄它們?還有:隨機部分在哪裏進來? – Nobody

+0

爲了清晰起見,對隨機部分進行了編輯。 mot文件不會被丟棄。他們被移動到目標目錄。 –

+1

我的意思是:ls選擇所有.sct和.mot文件,只需選擇.sct文件即可。當沒有雙重.sct文件(雙重含義如:123-12345-00.sct和123-12345-01.sct)時,您甚至不必按此方式排序和uniq。 – Nobody

1
#! /usr/bin/env bash 

dir="$1" 
count="$2" 

[ "$dir" ] && [ $count -gt 0 ] && { 

    if [ ! -d "$dir" ]; then echo "$0: $dir: no such directory"; exit; fi; 

    RANDOM=$(date +%s)      #init random seed 

    for ((c=0; c < $count; c++)); do 

     files=(*.sct)      #creates array of sct files 
     ct=${#files[@]}      #computes array length 

     if [ $ct -eq 0 ]; then break; fi #no more .sct file, exiting 

     sct=${files[$[($RANDOM % $ct)]]} #pick random file 

     # You might want to change this according to your file names 
     # Everything before the last dash `-' (included) will be taken 
     # as prefix 
     prefix=$(echo $sct | sed 's:\(.*-\).*:\1:') 

     mot_files=($prefix*.mot)   #creates array of all matching .mot 

     mv $sct $dir      #moves .sct to $dir 
     if [ ${#mot_files[@]} -gt 0 ]; then 
      mv ${mot_files[@]} $dir   #moves each matching .mot to $dir 
     fi 

    done 

} || echo "usage: $0 <dir> <num of files>" 

會做到這一點。


 
/tmp/r > ls 
123-12345-00.mot 123-12345-05.mot 123-12346-04.mot 
123-12345-00.sct 123-12346-00.mot 123-12346-05.mot 
123-12345-01.mot 123-12346-00.sct 123-12348-00.mot 
123-12345-02.mot 123-12346-01.mot 123-12348-00.sct 
123-12345-03.mot 123-12346-02.mot foo 
123-12345-04.mot 123-12346-03.mot 
/tmp/r > mkdir bar 
/tmp/r > ./foo bar 2 
/tmp/r > ls 
123-12346-00.mot 123-12346-02.mot 123-12346-05.mot 
123-12346-00.sct 123-12346-03.mot bar 
123-12346-01.mot 123-12346-04.mot foo 
/tmp/r > ls bar 
123-12345-00.mot 123-12345-02.mot 123-12345-05.mot 
123-12345-00.sct 123-12345-03.mot 123-12348-00.mot 
123-12345-01.mot 123-12345-04.mot 123-12348-00.sct 
+0

+1:太棒了!斯科特,你欠伯特蘭一瓶最好的香檳!祝你們好運。 – shellter

0

這裏是我會怎麼處理這:

#!/bin/bash 
## use the first param as count (1 as default value) 
count=${1:-1} 
## list all .sct files, random sort and pick the first $count 
ls $SRCDIR/*.sct | sort -R | head -n $count | 
    while read file; do 
     ## for each file, figure out the prefix and move prefix* 
     prefix="${file%-*}-" 
     mv -v $prefix* $DESTDIR 
    done 

編輯:最初我錯過了其中的文件的數量是一個參數,現在已經更新腳本中的一部分。爲了清晰起見,我掃描了設置SRCDIR和DESTDIR的部分。