将文件的每一行传递给awk,并将每一列作为单独的变量以运行命令。

huangapple go评论56阅读模式
英文:

Bash Getting each line of file to awk and have each column as a separate variable to run a command

问题

抱歉,你的请求有点复杂,但我会尽力提供有关代码的翻译和问题的解答。以下是代码的翻译:

#!/bin/bash

echo "您已打开了Roulette Dealer Finder程序!"
echo "您是否有日期和时间的列表?如果是这样,请使用文件作为参数运行此脚本"
echo "确保其格式为MMDD TIME"

# 如果没有提供文件作为参数,询问用户日期、小时和AM/PM
if [ -z "$1" ]
then
        echo "输入日期为MMDD:"
        read date

        echo "输入小时为##:"
        read hour

        echo "这是AM还是PM?"
        echo "1) AM"
        echo "2) PM"
        read meridiem

        if [ "${meridiem}" = 1 ]; then
                echo "以下是${hour} AM的Roulette荷官:"
                awk -F" " '{print $1,$2,$5,$6}' ${date}_Dealer_schedule.txt | grep -i "${hour}:00:00 AM"

        elif [ "${meridiem}" = 2 ]; then
                echo "以下是${hour} PM的Roulette荷官:"
                awk -F" " '{print $1,$2,$5,$6}' ${date}_Dealer_schedule.txt | grep -i "${hour}:00:00 PM"
        else
                echo "您没有从可用选项中选择..."
                echo "您选择的是:${meridiem}"
                echo "请下次选择'1'或'2'。"
        fi

# 现在我们要对文件中的每一行执行操作
else
        # 慢慢地失去理智,直到它起作用或大脑爆炸
fi

请注意,我只提供了代码的翻译,不包括问题的解答。如果您需要关于代码的特定问题的帮助,请提出具体问题,我会尽力提供解答。

英文:

Im trying to get this to take each line of a file that is formatted like 0310 05:00:00 AM and use the 0310 to open ####_Dealer_schedule.txt and then grep the time followed by adding each result to an output file. I've attempted to redo it repeatedly but end up scrapping the result as its just not working.

Code:

#!/bin/bash


echo "You have opened the Roulette Dealer Finder Program!"
echo "Do you have a list of dates and time? If so, run this script with the file as the arg"
echo "Make sure it is formatted as MMDD TIME"

#if the didnt give a file just ask them all the things
if [ -z "$1" ]
then
        echo "Enter Date as MMDD:"
        read date

        echo "Enter the Hour as ##:"
        read hour

        echo "Is this AM or PM?"
        echo "1) AM"
        echo "2) PM"
        read meridiem

        if [ "${meridiem}" = 1 ]; then
                echo "Here is the Roulette Dealer for" ${hour} "AM:"
                awk -F" " '{print $1,$2,$5,$6}' $date\_Dealer_schedule.txt | grep -i "${hour}:00:00 AM"

        elif [ "${meridiem}" = 2 ]; then
                echo "Here is the Roulette Dealer for" ${hour} "PM:"
                awk -F" " '{print $1,$2,$5,$6}' $date\_Dealer_schedule.txt | grep -i "${hour}:00:00 PM"
        else
                echo "You didnt pick from the available options..."
                echo "you selected: "${meridiem}
                echo "Please select either '1' or '2' next time."
        fi

# Now we want to do the thing for each of the lines in the file
else
        #slowly lose mind until it works or brain explodes
fi

List File:

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

Here is a list of what I have tried:

Testvar:

        testvar=$(awk '{print $1}' $1 | uniq)

        head ${testvar}\_Dealer_schedule.txt

This output:

head: cannot open '0310' for reading: No such file or directory
head: cannot open '0312' for reading: No such file or directory
==> 0315_Dealer_schedule.txt <==
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

Xargs:

        xargs < $1 -n 2 | awk -F" " '{print $1,$2}' > tempt.txt
        wc tempt.txt 
        awk -F" " '{print $1}' temp.txt
        #awk -F" " '{print $2}' temp.txt

this output:

 20  39 222 tempt.txt
awk: fatal: cannot open file `temp.txt' for reading (No such file or directory)

Array:

        i=0
        while read line
        do 
          array[$((++i))]=$line
          echo ${array[$i]}
        done < $1

This output:

find_the_dealer.sh: 37: array[0]=0310: not found
find_the_dealer.sh: 38: Bad substitution

答案1

得分: 2

你会用 `awk` 做什么?当然 `awk` 可以打开和处理额外的文件(而不仅仅是标准输入),但在纯Bash中做这件事更容易一些。这是一个丑陋而低效的黑客,用来展示基本原理:

```bash
while read -r prefix time meridiem discard; do
  grep -- "^${time} ${meridiem}" \
          "/path/to/${prefix}_Dealer_schedule.txt"
done < /path/to/list > /path/to/output

现在让我们稍微变得更加合理和高效(减少 grep 调用次数,减少文件遍历):

declare -A prefix_times=()

while read -r prefix time meridiem discard; do
  prefix_times["$prefix"]+="|${time} ${meridiem}"
done < /path/to/list

for prefix in "${!prefix_times[@]}"; do
  grep -E -- "^(${prefix_times["$prefix"]:1})" \
             "/path/to/${prefix}_Dealer_schedule.txt"
done > /path/to/output
英文:

What would you use awk for? Of course awk can open and handle additional files (other than its standard input), but this is somewhat easier to do in pure Bash. Here’s an ugly, inefficient hack, to show the basic principle:

while read -r prefix time meridiem discard; do
  grep -- "^${time} ${meridiem}" \
          "/path/to/${prefix}_Dealer_schedule.txt"
done < /path/to/list > /path/to/output

Now let’s make this↑ slightly more reasonable and efficient (fewer grep invocations, fewer file traversals):

declare -A prefix_times=()

while read -r prefix time meridiem discard; do
  prefix_times["$prefix"]+="|${time} ${meridiem}"
done < /path/to/list

for prefix in "${!prefix_times[@]}"; do
  grep -E -- "^(${prefix_times["$prefix"]:1})" \
             "/path/to/${prefix}_Dealer_schedule.txt"
done > /path/to/output

huangapple
  • 本文由 发表于 2023年7月6日 16:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定