簡體   English   中英

如何在循環中將“ cut”命令的輸出附加為文件的下一列?

[英]how do i append output of “cut” command as next column of a file in loop?

我需要從文件中剪切出一列,並將其附加到另一個文件作為其下一列。 它必須循環執行,直到滿足特定條件為止。 我正在嘗試以下代碼

while(limit<inFeatures){
            memset(outFile,'\0',499);
            strcpy(outFile,outputPath);
            strcat(outFile,"Top_");
            sprintf(buffer,"%d",limit);
            strcat(outFile,buffer);
            strcat(outFile,"_features-FVT.csv");
            int pos;
            for(i=0;i<limit;i++){
                pos=boolian[i]+1;
                memset(cmd,'\0',999);
                strcpy(cmd,"cut -d ',' -f");
                sprintf(buffer,"%d",pos);
                strcat(cmd,buffer);
                strcat(cmd," ");
                strcat(cmd,inputFVT);
                strcat(cmd," >> ");
                strcat(cmd,outFile);
                system(cmd);
            }
}

當我執行此操作時,結果將附加在輸出文件的底部。 我如何將結果附加到輸出文件的下一列...?

感謝那些在哪里幫助我的人。 我成功完成了該代碼。

inFile=fopen(inputFVT,"r");
        //printf("\n%s",inputFVT);//For debugging purpose
        if(inFile==NULL){
            printf("1.Cannot open the input File");exit(0);
        }

            count = 0;
            //int retVal;

            while(count<totalFeatures){
                tot_features[count]=(char *)calloc(50,sizeof(char));    
                memset(tot_features[count],'\0',49);
                int retVal = fscanf(inFile,"%[^,]",tot_features[count]);
                fflush(inFile);
                ch = fgetc(inFile);
                fflush(inFile);
                if(retVal == -1) break;
                //printf("1. %s\n", tot_features[count]); //For debugging purpose
                count = count + 1;

            }
                count = 0;
        inputFeatures=fopen(argv[2],"r");
        //printf("\n%s",argv[2]);//For debugging purpose
        if(inputFeatures==NULL){
            printf("2.Cannot open the input File");exit(0);
        }
        count = 0;
        //int retVal;

        while(count<inFeatures){
            in_features[count]=(char *)calloc(50,sizeof(char));
            memset(in_features[count],'\0',49);
            int retVal=fscanf(inputFeatures,"%[^\n]",in_features[count]);
            fflush(inputFeatures);  
            if(retVal == -1) break;
            //printf("2. %s\n", in_features[count]); //For debugging purpose
            ch = fgetc(inputFeatures);
            fflush(inputFeatures);  
            count = count + 1;
        }
            count = 0;
        for(i=0;i<inFeatures;i++){
            for(j=0;j<totalFeatures;j++){
                if((strcmp(in_features[i],tot_features[j]))==0){
                    //printf("3. \n%s %s",in_features[i],tot_features[j]);//For debugging purpose
                    boolian[i]=j+1;
                    break;
                }else{
                    boolian[i]=-1;
                }
                count++;
            }

        }

        int limit=100;
        char *buffer,temp[5];
        char outFile[500];
        //char cmd[1000];
        buffer=(char*)calloc((500+count+count),sizeof(char));
        while(limit<inFeatures){
            memset(outFile,'\0',499);
            strcpy(outFile,outputPath);
            strcat(outFile,"Top_");
            sprintf(temp,"%d",limit);
            strcat(outFile,temp);
            strcat(outFile,"_features-FVT.csv");
            strcpy(buffer,"cut -d ',' -f ");
            for(i=0;i<limit;i++){
                sort[i]=boolian[i];
            }
            quicksort(sort,0,limit-1);
            int pos;
            for(i=0;i<limit;i++){
                pos=sort[i];
                sprintf(temp,"%d,",pos);
                strcat(buffer,temp);
            }
            sprintf(temp,"%d",totalFeatures+1);
            strcat(buffer,temp);

            strcat(buffer," ");
            strcat(buffer,inputFVT);
            strcat(buffer," > ");
            strcat(buffer,outFile);
            //printf("\n%s",buffer);//Debuging code
            system(buffer);
            limit=limit+100;
        }
        strcpy(outFile,outputPath);
        strcat(outFile,"Top_");
        sprintf(temp,"%d",inFeatures);
        strcat(outFile,temp);
        strcat(outFile,"_features-FVT.csv");
        strcpy(buffer,"cut -d ',' -f");
        for(i=0;i<inFeatures;i++){
                sort[i]=boolian[i];
            }
            quicksort(sort,0,inFeatures-1);
        int pos;
            for(i=0;i<inFeatures;i++){
                pos=sort[i];
                sprintf(temp,"%d,",pos);
                strcat(buffer,temp);
            }
            sprintf(temp,"%d",totalFeatures+1);
            strcat(buffer,temp);
            strcat(buffer," ");
            strcat(buffer,inputFVT);
            strcat(buffer," > ");
            strcat(buffer,outFile);
            system(buffer);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM