English 中文(简体)
• 如何调换数量,与气球和气球相匹配?
原标题:How to move a number and match with column and rows in linux?
  • 时间:2011-11-22 10:05:46
  •  标签:
  • linux
  • awk

Hi guys i have a question??I want to move column in another file and match with rows and column and put in exact position.

投入文件1是:

COURSE NAME: Java
CREDITS: 4
200345    88
300126    78
287136    68
200138    71
COURSE NAME: Operating System
CREDITS: 4
287136    86
200138    72
200345    77
300056    78

投入文件2是:

STUDENT ID        Java        Operating System      GPA
200138
200345
287136
300056
300126

需要产出:

STUDENT ID        Java        Operating System      GPA
200138             71                 72
200345             88                 77
287136             68                 86
300056             -                  78
300126             78                 -

我在谈到这一法典:

awk NR==FNR{a[$1]=$2;next} {print $0 FS a [$1];next} file1 file2

其产出如下:

STUDENT ID       JAVA       Operating Systems       GPA
200138 72
200345 77
287136 86
300056 78
300126 78

I tried alot:( Can you please help me?

最佳回答

您可以使用<代码>awk,但并非微不足道。 与此类似:

# We will save every course name in an array for the report, but first remove the 
# the unwanted space from it s name. This line only works on the COURSE NAME lines
/^COURSE NAME/ {cn=gensub(" ","_","g",gensub(".*: ","","g",$0)); crss[cn]+=1 }

# On lines which starts with a number (student id), we are saving the student ids 
# in an array (stdnts) and the students score with a "semi" multideminsional array/hash
# where the indecies are "student_id-course_name" 
/^[0-9]/ {stdnts[$1]+=1 ; v[$1 "-" cn]=$2}

# after the above processing (e.g. the last line of the input file)
END {
      # print the header, it s dynamic and uses the course names it saved earlier
      printf("%-20s","STUDENT ID");
      for (e in crss) {printf("%-20s",e)}
      printf("%-20s
","GPA")

      # print the report
      for (s in stdnts)
          # we need to print every student id
          { printf("%-20s",s)
            for (cs in crss) {
                # then check if she had any score for every score, and print it
                if (v[s "-" cs] > 0) {
                    printf("%-20s",v[s "-" cs])
                }
                else {
                    printf("%-20s","-")
            }
          }
        printf("%-20s
"," ")
      }
  }

见以下行动:

  1. the script is not optimized;
  2. it replaces the original course names with a spaceless one
  3. the output of the students table is not sorted by student id
  4. it need only the first file as input! Put the above in a file like report.awk, then do awk -f report.awk INPUT_FILE.

HTH

问题回答

i 撰写了快速和 d脏的解决办法,为你提供实例。

指挥:

 sed  /CREDIT/d  file1|awk  FNR==NR{ if($0~/Java/){j++;o=0;next;}
        if($0~/Operating/){o++;j=0;next;}
        if(j){java[$1]=$2}
        if(o){os[$1]=$2}
}NR>FNR{OFS=" ";
        if(FNR==1){sub(/ /,"");print;}
        else{$2=" "
        $3=($1 in java)?java[$1]:"-";
        $4=($1 in os)?os[$1]:"-";
        print $0;
        }

}  - file2|column -t|sed -e  s/ID/ ID/  -e  2,${s/ /    /} 

• 测试我的 con子:

kent$  sed  /CREDIT/d  file1|awk  FNR==NR{ if($0~/Java/){j++;o=0;next;}
        if($0~/Operating/){o++;j=0;next;}
        if(j){java[$1]=$2}
        if(o){os[$1]=$2}
}NR>FNR{OFS=" ";
        if(FNR==1){sub(/ /,"");print;}
        else{$2=" "
        $3=($1 in java)?java[$1]:"-";
        $4=($1 in os)?os[$1]:"-";
        print $0;
        }

}  - file2|column -t|sed -e  s/ID/ ID/  -e  2,${s/ /    /} 
STUDENT ID  Java  Operating  System  GPA
200138        71    72
200345        88    77
287136        68    86
300056        -     78
300126        78    -

事实上,逻辑部分相对容易,许多守则只是以你为例,以同样的格式编制产出。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签