这个脚本是否适合给出预期的输出?

时间:2017-05-24 07:02:47

标签: unix awk aggregate

此脚本是否会提供预期的输出。提供的文件是示例文件(小文件) 在尺寸方面)。需要从输入文件中查找列名称并根据 列名称对其进行聚合以生成报告。如果不是那么 将是可能的解决方案。

   #!/bin/sh
    temp_file1=$(mktemp /tmp/temp_file1.XXXXX)
    contact=""
    not_Type=""
    count=""
    awk 'BEGIN{
        OFS=FS=","
           split(target,fields,FS)
              for (i in fields)
                  print i
                     field_idx[fields[i]] = i
                     print field_idx[fields[i]]
                       }
                      NR==1 {
                         for (i=1;i<=NF;i++)
                              head[i] = $i
                              print $head[i]
                              next
                            } ' $1 > temp_file1
    myarr=( $( cat temp_file1 ) )  # to get the columns name in temp file

    for i in ${myarr[*]}    #To check required columns to do aggregations
    do
            case i in
              Contact_Id)
                contact=Contact_id ;;
              Not_Type)
                not_Type=Not_Type ;;
              Count)
                count=Count ;;
              esac
    done

实际聚合逻辑

awk 'BEGIN{FS=OFS=","}{a[$contact OFS $not_Type)]+=1}END{for(i in a)print i,a[i]}'  $1 > $2

注意: - 对于awk命令

,它在第1行给出错误
  

awk: 0602-562 Field $() is not correct.
  The input line number is 1.
  The source line number is 12.
  a.sh[20]: 0403-057 Syntax error at line 20 : '(' is not expected.

输入: -

Sr_No,Contact_Id,Not_Type,Count

1,A,RC,1
2,B,OTC,1
3,C,RC,1
4,A,OTC,1
5,D,PB,1
6,A,RC,1
7,B,OTC,1

预期OutPut: -

Sr_No,Contact_Id,Not_Type,Count

A,OTC,1
A,RC,2
B,OTC,2
C,RC,1
D,PB,1

提前致谢。

0 个答案:

没有答案