简体   繁体   中英

How to move a number and match with column and rows in linux?

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.

input file1 is:

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

input file2 is:

STUDENT ID        Java        Operating System      GPA
200138
200345
287136
300056
300126

need output like this:

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

I am tring this code :

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

and its output will come like this:

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

I tried alot:( Can you please help me?

Well you can do it with awk but it's not trivial. Like this:

# 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\n","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\n"," ")
      }
  }

See here in action: https://ideone.com/AgaS8

Note :

  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 wrote a quick and dirty solution, works for your given example input.

The command:

 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/ /    /}'

test on my console:

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    -

Actually the logic part is relative easy, a lot of codes are just for making the output in the same format in your example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM