0

I am a newbie to Unix. Learning on my own.....I am writing a simple shell script to pull the Name of a dealer who was working a shift at the specified time where search takes place from multiple files within a directory.

Idea is I need to be able to pass Date of File e.g. 0310 or time in File e.g. 05 or time type from file AM or PM to pull specific dealer who was working at that time.

I am able to pull data using the following script

grep -i '05' 0310* | awk -F " " '{print $(01) , $(02) , $(05) , $(06)}' | grep -i am > Dealers_working_during_losses.txt

This gives me names of all dealers that are listed in file names 0310_Dealer_Analysis at 05 AM. Now I need to create a script so I can provide date, time & time type as an argument so a script can be used by anyone. Please advise how to proceed ? Since I am new bee , would appreciate simple solution so I can understand the logic rather than something complex....

Below is sample file data. I only need to pull data for Roulette dealer.

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
6
  • Welcome! Can you provide a sample of the source text? – schrodigerscatcuriosity Mar 24 '20 at 3:28
  • Here is the file ......I only need to pull the Roulette dealer name. 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 – cyberwaala Mar 24 '20 at 3:33
  • Please post it in the question, in comments it's illegible :). edit the question and the format the text to code with the code tool {}. – schrodigerscatcuriosity Mar 24 '20 at 3:36
  • added to question – cyberwaala Mar 24 '20 at 3:37
  • The separator between names is a tab? In the text you posted they are all spaces, then we can't separate a person's name from the other. Can you post it with tabs or something? – schrodigerscatcuriosity Mar 24 '20 at 3:44
0

First let's clean up your existing script:

1) The -i arg to grep is for a lower case alphabetic comparison so it doesn't make sense to use it on numbers. So just get rid of the -i from grep -i '05' 0310*.

2) " " is the default FS value for awk so you don't need to specify that with -F " " - just get rid of that too.

3) The input field numbers don't need to be 2-digit, nor do they need to be wrapped in parentheses to delimit them so every $(01) etc. can just be $1 etc.

4) You never need grep when you're using awk so this:

grep '05' 0310* |
awk '{print $1, $2, $5, $6}' |
grep -i am > Dealers_working_during_losses.txt

can be written as just:

awk '/05/{$0=$1 OFS $2 OFS $5 OFS $6; if (tolower($0) ~ /am/) print}' 0310* > Dealers_working_during_losses.txt

Now let's fix existing bugs:

5) /05/ will find 05 anywhere in the record (as a minutes field for example) but you only want to find it at the start of the record as that's where the hour specification exists so change that to /^05/.

6) Similarly, just like grep -i am did, tolower($0) ~ /am/ will find am anywhere in the record (in a persons name for example) but you only want to find it when it's the 2nd field in the record so change that to tolower($2) == "am"

We now have this command:

awk '/^05/{$0=$1 OFS $2 OFS $5 OFS $6; if (tolower($2) == "am") print}' 0310* > Dealers_working_during_losses.txt

but since we're only testing $2 for am or pm, we don't need to build a whole new record ($0) first before testing then printing it and can instead do:

awk '/^05/{if (tolower($2) == "am") print $1, $2, $5, $6}' 0310* > Dealers_working_during_losses.txt

which we can write more idiomatically as:

awk '/^05/ && (tolower($2) == "am") {print $1, $2, $5, $6}' 0310* > Dealers_working_during_losses.txt

And now to how to create a shell script that accepts arguments for all of the values. That'd be:

#!/usr/bin/env bash

awk -v time="$2" -v ampm="$3" '($0 ~ ("^"time)) && (tolower($2) == tolower(ampm)) {print $1, $2, $5, $6}' "$1"* > Dealers_working_during_losses.txt

which, assuming the above is stored in an executable file named foo that's in your PATH, you'd call as:

foo 0310 05 am

The only thing that I think is non-obvious above is that we had to use a different syntax for the time comparison when we went from testing a literal value (^05) to a string produced by concatenating the string "^" with the value of the variable time because use of a string in a regexp context requires us to use a dynamic regexp which also means that the shortcut /.../ for $0 ~ /.../ that works in constant regexp comparisons cannot be used so we need to write the $0 ~ part too.

1
  • 1
    Thx Ed...ill check this shortly !!!! – cyberwaala Mar 24 '20 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.