2012-02-06 44 views
0

解決:我的一個好朋友寫了下面的程序對我來說:拆分一個大的文本文件轉換成列

filename="my_input_file" 
context="channel"    # this is the key which separates the blocks in the input file 
desired_column_separator="," # this will separate the columns in the output file 
output_prefix="modified_"  # prefix for the output file 


if [ -d ./tmp ] 
then 

echo " " 
echo "***WARNING***" 
echo "I want to use and delete a ./tmp/ directory, but one already exists... please remove/rename it, or alter my code***" 
echo " " 
exit 
fi 


mkdir ./tmp 
cd ./tmp 

csplit -z -n 4 ../$filename /$context/ {*} 1> /dev/null 

filenum=`ls -1 ./ | wc -l` 
limit=`echo "$filenum - 1" | bc -l` 
lines=`wc -l < xx0000` 

touch tmp.dat 


     for j in `seq 1 $lines` 
     do 

    oldstring='' 

       for i in `seq 0 $limit` 
       do 

       inputNo=`printf "%04d" $i` 
       string=`head -n $j 'xx'$inputNo | tail -n 1` 

     oldstring=$oldstring$string$desired_column_separator 

       done 

     finalstring=`echo $oldstring | tr -d '\r' | tr -d '\n'` 

     echo "working on line "$j" out of "$lines 
       echo -n $finalstring >> tmp.dat     
       echo -e "\r" >> tmp.dat 

     done 

mv tmp.dat ../$output_prefix$filename 
cd .. 
rm -r -f ./tmp/ 

echo "...done!" 

原文:我知道分裂的文本文件已經完成,在這個論壇的死亡,但我不能找不到特定於我的問題的方法。 我想將一個大文件(> 200mb)分割成文本行的列,但'split'函數會將每一列放入它自己的文件中。說實話,3,000多個單獨的文件文本是加載到其他程序中的一種痛苦。最重要的是,我還想提取文本文件的一部分作爲我的數據的標題(第4行的最後一部分)。 最初文件由一列的,就像這樣:

channel names: 
03/02/2012 12:03:03 - TDS3k(CH1) 
start times: 
03/02/2012 12:08:02.311422 
dt: 
0.000000 
data: 
-8.000000E-4 
-8.000000E-4 
-1.600000E-3 
... (9,994 lines omitted) 
-2.400000E-3 
-1.600000E-3 
-2.400000E-3 
channel names: 
03/02/2012 12:03:03 - TDS3k(CH1) 
start times: 
03/02/2012 12:33:11.169533 
dt: 
0.000000 
data: 
-8.000000E-4 
-1.600000E-3 
-1.600000E-3 
... (another 9,997 lines omitted) 

我想它看起來像這樣:

channel names:      channel names: 
03/02/2012 12:03:03 - TDS3k(CH1) 03/02/2012 12:03:03 - TDS3k(CH1) 
start times:      start times: 
03/02/2012 12:08:02.311422   03/02/2012 12:33:11.169533 
dt:        dt: 
0.000000       0.000000 
data:        data: 
-8.000000E-4      -8.000000E-4 ... 
-8.000000E-4      -1.600000E-3 ... 
-1.600000E-3      -1.600000E-3 ... 
...        ... 

我懷疑越來越分裂在正確的地方是比較容易做的比標題,但我不夠好做任何一個。

在此先感謝

編輯:我還沒有使用任何特定的語言。我只需要一種格式的數據,我可以在R中進行分析。我會根據你們的建議來解決這個問題。

+1

你想使用什麼編程語言? – ChrisWue 2012-02-06 00:11:24

+0

您知道,您可能只想提供一點關於您正在使用的工具(語言/電子表格/數據庫/其他)的提示。 – 2012-02-06 00:11:48

+0

我沒有使用任何特定的語言。 'sed'和'awk'都是朋友建議的,但我無法讓他們工作。我會在某些時候將它加載到R中。 – user1191276 2012-02-06 03:32:33

回答

0

你在用什麼語言?每個條目有多少個「數據」條目?

使用python,最簡單的方法是首先將數據分解爲「條目」,然後爲每個條目編寫一個解析函數,以僅生成您希望在最終輸出中看到的值。然後簡單地加入最終輸出,或使用csv模塊寫入。

input = """channel names: 
03/02/2012 12:03:03 - TDS3k(CH1) 
start times: 
03/02/2012 12:33:11.169533 
dt: 
0.000000 
data: 
-8.000000E-4 
-1.600000E-3 
-1.600000E-3 
channel names: 
03/02/2012 12:03:03 - TDS3k(CH1) 
start times: 
03/02/2012 12:33:11.169533 
dt: 
0.000000 
data: 
-8.000000E-4 
-1.600000E-3 
-1.600000E-3 
""" 

LINES_PER_ENTRY = 10 

def parseEntry(entry): 
    return entry 

raw = input.split('\n') 

entries = [raw[i*LINES_PER_ENTRY:(i+1)*LINES_PER_ENTRY] for i in range(len(raw)/10)] 


parsed_entries = [parseEntry(entry) for entry in entries] 

outfile = open('outfile.txt','w') 
for parsed_entry in parsed_entries: 
    outfile.write('\t'.join(parsed_entry) + "\n") 
print parsed_entries 
+0

我正在嘗試使用R進行分析,但我需要首先以正確格式的數據。我會去用任何語言工作。一位朋友建議'sed'和'awk',但我還沒有能夠讓他們中的任何一個人工作。 這會輸出數據作爲單個文件中的列嗎?每個輸出列(包括文本和標題)的總行數應該爲10007,但可能會有所不同,因此使用固定的條目長度是有風險的。感謝您的幫助,明天我醒來時我會看看Python。 另外,什麼是「5分鐘編輯」的東西?這真的很煩人。 – user1191276 2012-02-06 00:57:29

+0

在這種情況下,你能提供一個你想要解析的更大的例子嗎? – 2012-02-06 19:55:24

+0

如果你想,但我不知道它會做什麼。也許我可以更好地描述它: 我的數據由一列中的幾個「實例」組成。每個「實例」由7行文本組成(如op所示),然後是10,000行數字。 '實例'是連接的。 – user1191276 2012-02-07 10:37:17