询问用户输入并扫描到某个文件并输出结果(带有重复项)

时间:2017-01-31 05:43:17

标签: bash shell unix duplicates unique

我需要提示用户输入来自CSV文件的类的名称:

Computer,Course 1,This course is taught by Dr. Chen
Information technology,Course 2,This course is taught by Dr. Weiss
Database,Course 3,This course is taught by Dr. Gonzalas
Algorithm,Course 4,This course is taught by Dr. Sanabria
Computer,Course 5,This course is taught by Dr. Sun
Data mining,Course 6,This course is taught by Dr. Li
Algorithm,Course 8,This course is taught by Dr. Xue

用户将输入第1列中的一个项目。如您所见,有重复项。需要有一种方法来提示用户澄清他将要求哪些类的信息,然后继续选择。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

请注意,这只是一个草稿,它在我的系统上运行,尚未经过其他测试

#!/bin/bash

IFS=$"\n"

# Read user input
read -p "Type a name of class: " class

# Results in two views - entries count and entries content
entries_cnt=`cat ./tst.csv | cut -d',' -f1 | grep -cw "$class"`
entries_nfo=$(cat tst.csv | awk -v class=$class -F"," '{if ($1 ~ class) print $0 }')

# If one entry founded
if [ "$entries_cnt" == "1" ] ; then
    echo 'Entry founded:'
    echo $entries_nfo

# If entry not founded
elif [ "$entries_cnt" \< "1" ] ; then
    echo 'Entry not found'

# If founded more than 1 entry
else
    echo 'Founded more than 1 entry:'
    echo $entries_nfo;
    echo 'Available courses:'
    echo $entries_nfo | cut -d',' -f2

    # Read user input
    read -p 'Input course: ' course
    echo $entries_nfo | awk -v course=$course -F"," '{if ($2 ~ course) print $0 }'
fi

答案 1 :(得分:0)

我能够完成很多工作,其中一些组合可以帮助你。这就是我能够做到的:

#!/bin/sh
#Start your code from here


# Clear page and enter variables: date, files, etc.
clear
template=Template.email
d=$(date +%m-%d-%Y)

grep '[^[:blank:]]' < Dictionary.data > temp.csv
awk 'NR > 2 {print}' < temp.csv > temp2.csv

# Welcome message and display catalog/dictionary
echo "Hello and welcome to the Spring Term of 2017"
echo "Below are the classes available to you to enroll in."
echo "Select a course and we will email you further information"
echo "about the class."
echo "======================================================"

# Read file
while IFS=, read col1 col2 col3
do
    echo "$col1"
done < temp2.csv

# Ask for student email and class selection
echo ""
echo "Please enter your email: "
read email

echo "Please pick a class: "
read course

    # Open case switch 
    case $course in

        # Computer case
        "Computer")

            # Advise user about duplicates
            echo "There are two Computer courses with the same name. Please pick the one you like: "
            echo "1) Course 1 taught by Dr. Chen"
            echo "2) Course 5 taught by Dr. Sun"
            read detail

            # Ask user to pick which class he/she wants
            case $detail in

                # Display message and email user desciptions
                1)
                    echo "We have emailed you the course description. Thank you!"

                    sed -e "s/_CourseNumber_/Course 1/g" -e "s/__Description__/This course is taught by Dr. Chen/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
                    ;;

                2)
                    echo "We have emailed you the course description. Thank you!"

                    sed -e "s/_CourseNumber_/Course 5/g" -e "s/__Description__/This course is taught by Dr. Sun/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
                    ;;

                *) 

                    # Incorrect input
                    echo "Sorry that was not a correct choice."
                    break;;
            esac

            ;;

        # IT case   
        "Information technology")

            # Display message and email user desciptions
            echo "We have emailed you the course description. Thank you!"

            sed -e "s/_CourseNumber_/Course 2/g" -e "s/__Description__/This course is taught by Dr. Weiss/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
            ;;

        # Database case
        "Database")

            # Display message and email user desciptions
            echo "We have emailed you the course description. Thank you!"

            sed -e "s/_CourseNumber_/Course 2/g" -e "s/__Description__/This course is taught by Dr. Weiss/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
            ;;

        #Algorithm case
        "Algorithm")

            # Display message and email user desciptions
            echo "There are two Algorithm courses with the same name. Please pick the one you like: "
            echo "1) This course is taught by Dr. Sanabria"
            echo "2) This course is taught by Dr. Xue"
            read detail

            # Ask user to pick which class he/she wants
            case $detail in

                # Display message and email user desciptions
                1)
                    echo "We have emailed you the course description. Thank you!"

                    sed -e "s/_CourseNumber_/Course 4/g" -e "s/__Description__/This course is taught by Dr. Sanabria/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
                    ;;

                2)
                    echo "We have emailed you the course description. Thank you!"

                    sed -e "s/_CourseNumber_/Course 8/g" -e "s/__Description__/This course is taught by Dr. Xue/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
                    ;;

                *) 

                    # Incorrect input
                    echo "Sorry that was not a correct choice."
                    break;;
            esac

            ;;

        # Data Mining case
        "Data mining")

            # Display message and email user desciptions
            echo "We have emailed you the course description. Thank you!"

            sed -e "s/_CourseNumber_/Course 6/g" -e "s/__Description__/This course is taught by Dr. Li/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email
            ;;

        *) 

            # Incorrect input
            echo "Sorry that was not a correct choice.";;

    esac

    rm temp.csv
    rm temp2.csv
# End of file