linux 使用AWK GREP和PIPES在BASH中创建脚本,以分析文件并查找以下时间、AM/PM、第一个/最后一个ETC

wi3ka0sx  于 2022-11-22  发布在  Linux
关注(0)|答案(1)|浏览(116)

我需要创建一个脚本来查看每一天和发生的损失时间。

  • 时间
  • 上午/下午
  • 轮盘赌庄家的名字
  • 轮盘赌庄家的姓氏

例如,如果在3月10日下午2点发生了亏损,您将编写一个脚本来查找在该特定日期和时间工作的轮盘赌庄家。

$ cat 0310_Dealer_schedule 
Hour AM/PM  BlackJack_Dealer_FNAME LAST Roulette_Dealer_FNAME LAST  Texas_Hold_EM_dealer_FNAME LAST

12:00:00 AM Izabela Parrish Marlene Mcpherson   Madina Britton
01:00:00 AM Billy Jones Saima Mcdermott Summer-Louise Hammond
02:00:00 AM Summer-Louise Hammond   Abigale Rich    John-James Hayward
03:00:00 AM John-James Hayward  Evalyn Howell   Chyna Mercado
04:00:00 AM Chyna Mercado   Cleveland Hanna Katey Bean
05:00:00 AM Katey Bean  Billy Jones Evalyn Howell
06:00:00 AM Evalyn Howell   Saima Mcdermott Cleveland Hanna
07:00:00 AM Cleveland Hanna Abigale Rich    Billy Jones
08:00:00 AM Rahima Figueroa Billy Jones Madina Britton
09:00:00 AM Marlene Mcpherson   Cleveland Hanna Summer-Louise Hammond
10:00:00 AM Izabela Parrish Madina Britton  John-James Hayward
11:00:00 AM Madina Britton  Summer-Louise Hammond   Chyna Mercado
12:00:00 PM Summer-Louise Hammond   John-James Hayward  Katey Bean
01:00:00 PM John-James Hayward  Chyna Mercado   Evalyn Howell
02:00:00 PM Chyna Mercado   Billy Jones Cleveland Hanna
03:00:00 PM Katey Bean  Evalyn Howell   Rahima Figueroa
04:00:00 PM Evalyn Howell   Cleveland Hanna Billy Jones
05:00:00 PM Billy Jones Rahima Figueroa Summer-Louise Hammond
06:00:00 PM Rahima Figueroa John-James Hayward  John-James Hayward
07:00:00 PM Marlene Mcpherson   Chyna Mercado   Chyna Mercado
08:00:00 PM Saima Mcdermott Billy Jones Katey Bean
09:00:00 PM Abigale Rich    Evalyn Howell   Billy Jones
10:00:00 PM Evalyn Howell   Katey Bean  Cleveland Hanna
11:00:00 PM Cleveland Hanna Billy Jones Rahima Figueroa

这是0310代理商时间表文件
我已经知道损失发生在这几天

0310_win_loss_player_data:05:00:00 AM
0310_win_loss_player_data:08:00:00 AM
0310_win_loss_player_data:02:00:00 PM
0310_win_loss_player_data:08:00:00 PM
0310_win_loss_player_data:11:00:00 PM
0312_win_loss_player_data:05:00:00 AM
0312_win_loss_player_data:08:00:00 AM
0312_win_loss_player_data:02:00:00 PM
0312_win_loss_player_data:08:00:00 PM
0312_win_loss_player_data:11:00:00 PM
0315_win_loss_player_data:05:00:00 AM
0315_win_loss_player_data:08:00:00 AM
0315_win_loss_player_data:02:00:00 PM

我的问题是如何编写脚本来使用grep、管道和awk隔离所需的数据

第一次
这是我一直在尝试的,我不确定如何使用特定的命令来获得我需要的结果。

sshcrbum

sshcrbum1#

下面将演示实现所需的逻辑类型。
下面提供的脚本按日期进行比较处理。如果您希望分析整个日期范围,则需要自己计算。

#!/bin/sh

DBG=0
ShowCorrelation=0
ShowStaff=0
Threshold=0

while [ $# -gt 0 ]
do
    case ${1} in
        --debug ) DBG=1 ; shift ;;
        --correlations ) ShowCorrelation=1 ; shift ;;
        --staff ) ShowStaff=1 ; shift ;;
        --threshold ) Threshold=$2 ; shift ; shift ;;
        * ) echo "\n\t Invalid option used on command line.  Only valid options:  [ --debug | --correlations | --staff | --threshold {} ] \n Bye!\n" ; exit 1 ;;
    esac
done

flagMultiples(){
    awk -v dbg="${DBG}" \
        -v thresh="${Threshold}" \
        -v showC="${ShowCorrelation}" \
        -v showS="${ShowStaff}" '\
    BEGIN{
        split("", present ) ;   ###  empty the array
        split("", staff ) ; ###  empty the array
        id=0 ;
    }
    {
        if( dbg == 1 ){ print $0 ; } ;

        present[NR,1]=sprintf("%s %s", $1, $2 ) ;
        present[NR,2]=sprintf("%s %s", $3, $4 ) ;
        present[NR,3]=sprintf("%s %s", $5, $6 ) ;
        present[NR,4]=sprintf("%s %s", $7, $8 ) ;

        for( j=2 ; j <= 4 ; j++ ){
            if( dbg == 1 ){ printf("\t\t j = %s\n", j ) ;  } ;
            addEntry=1
            for( k=1 ; k <= id ; k++ ){
                if( dbg == 1 ){ printf("\t\t\t k = %s\n", k ) ;  } ;
                if( present[NR,j] == staff[k,1] ){
                    if( dbg == 1 ){ printf(" present[%s,%s] = %s\n", NR, j, present[NR,j] ) ; } ;
                    addEntry=0 ;
                } ;
            } ;
            if( addEntry == 1 ){
                id++ ;
                staff[id,1]=present[NR,j] ;
                if( dbg == 1 ){ printf("%02d - %s\n", k, staff[k,1] ) ; } ;
            } ;
        } ;
    }
    END{
        if( showS == 1 ){
            print "\n Staff List:" ;
            for( k=1 ; k <= id ; k++ ){
                printf("\t%2d - %s\n", k, staff[k,1] ) ;
            } ;
        } ;

        print "\n Staff Presence:" ;
        for( i=1 ; i <= NR ; i++ ){
            for( j=1 ; j <= 4 ; j++ ){
                printf("\t%s ", present[i,j] ) ;
            } ;
            print ""
        } ;

        if( showC == 1 ){
            print "\n Correlation Count:" ; 
        } ;
        max=0 ;
        for( k=1 ; k <= id ; k++ ){
            staff[k,2]=0 ;
            for( i=1 ; i <= NR ; i++ ){
                for( j=2 ; j <= 4 ; j++ ){
                    if( present[i,j] == staff[k,1] ){
                        staff[k,2]++ ;
                    } ;
                } ;
            } ;
            if( staff[k,2] > max ){
                likely=k ;
                max=staff[k,2] ;
            } ;
            if( staff[k,2] >= thresh ){
                if( showC == 1 ){
                    printf("\t%d of %d  for %s\n", staff[k,2], NR, staff[k,1] ) ;
                } ;
            } ;
        } ;

        print "\n Most Likely:" ; 
        printf("\t%d of %d  for %s\n", staff[likely,2], NR, staff[likely,1] ) ;
    }'
}

SCHEDULE="0310_Dealer_schedule"
cat >${SCHEDULE} <<-!EnDoFiNpUt
Hour AM/PM  BlackJack_Dealer_FNAME LAST Roulette_Dealer_FNAME LAST  Texas_Hold_EM_dealer_FNAME LAST

12:00:00 AM Izabela Parrish Marlene Mcpherson   Madina Britton
01:00:00 AM Billy Jones Saima Mcdermott Summer-Louise Hammond
02:00:00 AM Summer-Louise Hammond   Abigale Rich    John-James Hayward
03:00:00 AM John-James Hayward  Evalyn Howell   Chyna Mercado
04:00:00 AM Chyna Mercado   Cleveland Hanna Katey Bean
05:00:00 AM Katey Bean  Billy Jones Evalyn Howell
06:00:00 AM Evalyn Howell   Saima Mcdermott Cleveland Hanna
07:00:00 AM Cleveland Hanna Abigale Rich    Billy Jones
08:00:00 AM Rahima Figueroa Billy Jones Madina Britton
09:00:00 AM Marlene Mcpherson   Cleveland Hanna Summer-Louise Hammond
10:00:00 AM Izabela Parrish Madina Britton  John-James Hayward
11:00:00 AM Madina Britton  Summer-Louise Hammond   Chyna Mercado
12:00:00 PM Summer-Louise Hammond   John-James Hayward  Katey Bean
01:00:00 PM John-James Hayward  Chyna Mercado   Evalyn Howell
02:00:00 PM Chyna Mercado   Billy Jones Cleveland Hanna
03:00:00 PM Katey Bean  Evalyn Howell   Rahima Figueroa
04:00:00 PM Evalyn Howell   Cleveland Hanna Billy Jones
05:00:00 PM Billy Jones Rahima Figueroa Summer-Louise Hammond
06:00:00 PM Rahima Figueroa John-James Hayward  John-James Hayward
07:00:00 PM Marlene Mcpherson   Chyna Mercado   Chyna Mercado
08:00:00 PM Saima Mcdermott Billy Jones Katey Bean
09:00:00 PM Abigale Rich    Evalyn Howell   Billy Jones
10:00:00 PM Evalyn Howell   Katey Bean  Cleveland Hanna
11:00:00 PM Cleveland Hanna Billy Jones Rahima Figueroa
!EnDoFiNpUt

LOSSES="03_LossReport"
cat >${LOSSES} <<-!EnDoFiNpUt
0310_win_loss_player_data:05:00:00 AM
0310_win_loss_player_data:08:00:00 AM
0310_win_loss_player_data:02:00:00 PM
0310_win_loss_player_data:08:00:00 PM
0310_win_loss_player_data:11:00:00 PM
0312_win_loss_player_data:05:00:00 AM
0312_win_loss_player_data:08:00:00 AM
0312_win_loss_player_data:02:00:00 PM
0312_win_loss_player_data:08:00:00 PM
0312_win_loss_player_data:11:00:00 PM
0315_win_loss_player_data:05:00:00 AM
0315_win_loss_player_data:08:00:00 AM
0315_win_loss_player_data:02:00:00 PM
!EnDoFiNpUt

SCAN="/tmp/tmp.$$.loss_windows"

for eventDate in $( cut -f1 -d\_ "${LOSSES}" | sort -n | uniq )
do
    echo "\n==================================================================\nEvent Date:  ${eventDate}"

    grep '^'"${eventDate}" "${LOSSES}" | cut -f2- -d\: |
    while read timeSlot
    do
        grep "${timeSlot}" "${SCHEDULE}"
    done | flagMultiples
done

生成的会话输出如下所示:

ericthered@OasisMega1:/0__WORK$ ./junk_41.sh --threshold 2 --correlations --staff

==================================================================
Event Date:  0310

 Staff List:
     1 - Katey Bean
     2 - Billy Jones
     3 - Evalyn Howell
     4 - Rahima Figueroa
     5 - Madina Britton
     6 - Chyna Mercado
     7 - Cleveland Hanna
     8 - Saima Mcdermott

 Staff Presence:
    05:00:00 AM     Katey Bean  Billy Jones     Evalyn Howell 
    08:00:00 AM     Rahima Figueroa     Billy Jones     Madina Britton 
    02:00:00 PM     Chyna Mercado   Billy Jones     Cleveland Hanna 
    08:00:00 PM     Saima Mcdermott     Billy Jones     Katey Bean 
    11:00:00 PM     Cleveland Hanna     Billy Jones     Rahima Figueroa 

 Correlation Count:
    2 of 5  for Katey Bean
    5 of 5  for Billy Jones
    2 of 5  for Rahima Figueroa
    2 of 5  for Cleveland Hanna

 Most Likely:
    5 of 5  for Billy Jones

==================================================================
Event Date:  0312

 Staff List:
     1 - Katey Bean
     2 - Billy Jones
     3 - Evalyn Howell
     4 - Rahima Figueroa
     5 - Madina Britton
     6 - Chyna Mercado
     7 - Cleveland Hanna
     8 - Saima Mcdermott

 Staff Presence:
    05:00:00 AM     Katey Bean  Billy Jones     Evalyn Howell 
    08:00:00 AM     Rahima Figueroa     Billy Jones     Madina Britton 
    02:00:00 PM     Chyna Mercado   Billy Jones     Cleveland Hanna 
    08:00:00 PM     Saima Mcdermott     Billy Jones     Katey Bean 
    11:00:00 PM     Cleveland Hanna     Billy Jones     Rahima Figueroa 

 Correlation Count:
    2 of 5  for Katey Bean
    5 of 5  for Billy Jones
    2 of 5  for Rahima Figueroa
    2 of 5  for Cleveland Hanna

 Most Likely:
    5 of 5  for Billy Jones

==================================================================
Event Date:  0315

 Staff List:
     1 - Katey Bean
     2 - Billy Jones
     3 - Evalyn Howell
     4 - Rahima Figueroa
     5 - Madina Britton
     6 - Chyna Mercado
     7 - Cleveland Hanna

 Staff Presence:
    05:00:00 AM     Katey Bean  Billy Jones     Evalyn Howell 
    08:00:00 AM     Rahima Figueroa     Billy Jones     Madina Britton 
    02:00:00 PM     Chyna Mercado   Billy Jones     Cleveland Hanna 

 Correlation Count:
    3 of 3  for Billy Jones

 Most Likely:
    3 of 3  for Billy Jones
ericthered@OasisMega1:/0__WORK$

相关问题