แต่ต้องการแค่ 3000 บรรทัดในวันเวลาเดียวกันจากทุกไฟล์มาใช้
จะจัดการมันอย่างไร ขืนนั่งทำมือ (do manually) คงเหนื่อยนัก
ต้องเขียน code จัดการมัน
เนื่องจากได้ตัดขาดจาก window มานานนม
ทุกอย่างจึงเกิดขึ้นบน Ubuntu หนึ่งในระบบปฏิบัติการแบบ Linux
Top layer ก็ถูกเขียนด้วย shell script ไว้สำหรับเรียกใช้ second layer ซึ่งเขียนด้วย C++
ใน shell script ก็ยัดชื่อไฟล์จะที่อ่านและที่จะบันทึกเข้าไปอย่างซื่อๆ ซึ่งมีรายละเอียดดังต่อไปนี้
########################
#!/bin/sh
# Written by Tossaporn Srisooksai
########################
for foo in 3 5 7 8 9
do
./rawdata_management 2007 03 23 00 00 3000 1 "sensorscope-meteo-$foo.txt" "$foo.csv"
done
for foo in 10 11 12 13 14 15 17 18 19 21 23 24 25 26 27 30 31 32 33 34 35 36 37 39 40 41 42 43 44 45 46 47 49 50 51 53 54 55 56 57 59 60 61 62 63 64 65 66 68 69 70 71 72 73 75 76 79 80 81 82 84 85 87 88 89 92 93 94 95 96 97 98 99
do
./rawdata_management 2007 03 23 00 00 3000 2 "sensorscope-meteo-$foo.txt" "$foo.csv"
done
for foo in 100 103 104 105 106 107 109 111 121 122 #201 202 203 204 206 207
do
./rawdata_management 2007 03 23 00 00 3000 3 "sensorscope-meteo-$foo.txt" "$foo.csv"
done
exit 0
========================
อย่าลืมเปลี่ยน mode ด้วยคำสั่ง sudo chmod a+x ก่อนที่จะรัน
ส่วน rawdata_management (second lalyer) ซึ่งถูกเขียนด้วย C++ เป็นดังนี้
/***************************************************/
/* Preparing raw data of sensor nodes for Octave */
/* Written by Tossaporn Srisooksai 30/9/2010 */
/***************************************************/
#include
#include
#include
#include
#include
int main (int argc, const char* argv[]) {
if (argc != 10)
{
cout << "\nUsage: < YYYY MM DD HH mm line_max mode input_file output_file> " << endl;
exit(0);
}
string line; //input file stream
string target_date; //checking start date
string target; //setting start date
char input_file[20];
char output_file[20];
int flag = 0; // flag of writing output file
int i = 0; // copied line counter
int i_max; // max of copied line
int mode; // mode of node number
int n; // pointer the space existed in line stream
int j;
//get the start date
target = argv[1];
target += ' ';
target += argv[2];
target += ' ';
target += argv[3];
target += ' ';
target += argv[4];
target += ' ';
target += argv[5];
//cout << target << endl;
// max of copied line
i_max = atoi(argv[6]);
// mode of node number
mode = atoi(argv[7]);
// open input and output file
ifstream inFile (argv[8]);
ofstream outFile (argv[9]);
if (inFile.is_open())
{
while (!inFile.eof() )
{
getline (inFile,line);
target_date.assign(line,mode+1,16); //check the start date
//cout << target_date << endl;
if ( target_date == target )
{ flag = 1; // output file can be written now
}
if (outFile.is_open())
{ if (( flag == 1) && (i < i_max ))
{
for (j=1; j<18; j++) // change space to ","
{
n=line.find_first_of(" ",1);
line.replace(n,1,",");
}
outFile << line << endl; //write line to output file
//cout << line;
i=i+1;
}
}
else cout << "Unable to open output file";
}
inFile.close(); //close file
outFile.close();
}
else cout << "Unable to open raw data file";
return 0;
}
========================
คอมไฟล์ไว้ที่โฟลเดอร์เดียวกับ shell script ด้วยคำสั่ง g++ -o rawdata_management rawdata_management.cpp
จบ........
ไม่มีความคิดเห็น:
แสดงความคิดเห็น