1

次の形式の 1 日の終わりの在庫データで構成されるファイルがいくつかあります。

ファイル名: NYSE_20120116.txt

<ticker>,<date>,<open>,<high>,<low>,<close>,<vol>
A,20120116,36.15,36.36,35.59,36.19,3327400
AA,20120116,10.73,10.78,10.53,10.64,20457600

すべてのシンボルのファイルを作成するにはどうすればよいですか? 例えばA社の場合

ファイル名 : A.txt

<ticker>,<date>,<open>,<high>,<low>,<close>,<vol>
A,20120116,36.15,36.36,35.59,36.19,3327400
A,20120117,39.76,40.39,39.7,39.99,4157900
4

1 に答える 1

2

最初のファイルをレコード レベルで分割し、最初のフィールドの値に基づいて各行を別のファイルにルーティングしますか?

 # To skip first line, see later
 cat endday.txt | while read line; do
     # Careful with backslashes here - they're not quote signs
     # If supported, use:
     # symbol=$( echo "$line" | cut -f1 -d, )
     symbol=`echo "$line" | cut -f1 -d,`

     # If file is not there, create it with a header
     # if [ ! -r $symbol.txt ]; then
     #    head -n 1 endday.txt > $symbol.txt
     # fi
     echo "$line" >> $symbol.txt
 done

あまり効率的ではありません: Perl または Python の方が優れていたでしょう。

ディレクトリに複数のファイルがある場合 (注意してください、それらを自分で削除する必要があります。そうしないと、何度も何度も処理されます...)、次のことができます。

 for file in *.txt; do
    echo "Now processing $file..."
    # A quick and dirty way of ignoring line number 1 --- start at line 2.
    tail -n +2 $file | while read line; do
       # Careful with backslashes here - they're not quote signs
       # If supported, use:
       # symbol=$( echo "$line" | cut -f1 -d, )
       symbol=`echo "$line" | cut -f1 -d,`

       # If file is not there, create it with a header
       # if [ ! -r $symbol.txt ]; then
       #    head -n 1 $file > $symbol.csv
       # fi
       # Output file is named .CSV so as not to create new .txt files
       # which this script might find
       echo "$line" >> $symbol.csv
    done
    # Change the name from .txt to .txt.ok, so it won't be found again
    mv $file $file.ok
    # or better move it elsewhere to avoid clogging this directory
    # mv $file /var/data/files/already-processed
 done
于 2012-07-03T12:27:55.703 に答える