Skip to main content
4 of 4
added 47 characters in body
Zac
  • 569
  • 4
  • 11
  • 17

How to use Cut and Grep command to find data separated by ":"

In my simple database, it contains text of like, example, soldiers.

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

And my script goes like this:

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

If I enter "Corporal" into name, the output will show

Tom Corporal:Recruit:Male:19:2
Nicole Sergeant:Corporal:Female:30:10

because the string matches under the section name and rank. How do i use delimiter : to categories the search, so if i enter my conditions under rank, it will search just the rank category and not everything else.

Thanks in advance.

And do up my question instead of down voting.

Zac
  • 569
  • 4
  • 11
  • 17