私のシンプルなデータベースには、たとえば兵士のようなテキストが含まれています。
Name:Rank:Gender:Age:Years of Service
Tom Corporal:Recruit:Male:19:2
Nicole Sergeant:Corporal:Female:30:10
Daniel Recruit:Sergeant:Male:40:19
私のスクリプトは次のようになります:
echo "Enter name: "
read name
echo "Enter rank: "
read rank
echo "Enter gender: "
read gender
echo "Enter age: "
read age
echo "Enter Years of service: "
read years
grep $name database.txt
名前に「Corporal」と入力すると、出力には次のように表示されます。
Tom Corporal:Recruit:Male:19:2
Nicole Sergeant:Corporal:Female:30:10
文字列がセクション名とランクの下で一致するためです。区切り文字 : を使用して検索を分類するにはどうすればよいでしょうか。ランクの下に条件を入力すると、ランク カテゴリだけが検索され、他のすべては検索されません。
前もって感謝します。
そして、私の質問に反対票を投じるのではなく、賛成票を投じてください。
答え1
grep "^.*:.*:$gender" Information.txt | awk -F: '{print $1}'