-1

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.

6
  • 1
    Do you need to find all the names of a given gender using the grep command? Can you better give a sample input and output that you expect so that it would be much more clear? Commented Jan 8, 2015 at 20:59
  • you can not use “” quotes you have to use " Commented Jan 8, 2015 at 21:55
  • I see nothing wrong except the quotes and that you already know field 3 as that is what you search for. It could also miss trigger, for example I know someone whose name is male, see is female. Commented Jan 8, 2015 at 22:01
  • the code does not work, and if i purely use grep $gender Information.txt, it will return all 3 information as in the gender section, it contains "male" and fe"male" Commented Jan 9, 2015 at 1:09
  • @Ramesh how else would i be able to do it ? Commented Jan 9, 2015 at 14:09

1 Answer 1

1

grep "^.*:.*:$gender" Information.txt | awk -F: '{print $1}'

1
  • this answer will probably be deleted with the question, but that is ok. Welcome! Commented Jan 8, 2015 at 22:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.