New user to UNIX scripting. Had a question I was stuck on.
I've been trying to make a script(for a larger project) that would search a file(lets say playerlist).
the file is already formatted into columns so it may look like
First name(1-10) Last Name(11-20) address (21-30)
now a user would be able to input a partial name, lets say "eter" and it can pull up Peter, however the search has to be restricted only to the first name column but the output itself has to include Peter's name AND address(skipping the last name)
I'm having a hard time displaying the information from that specific row after the search. I took the approach of using grep to search for the partial name after redirecting the 'first name' column to a temp file to isolate the search only to that column. But this is pretty much how far I got, I wasn't able to display the individual's address also.
So lets say I have a file that holds this information
Engineering Karen 58 John St. Peters
IT Roch 44 Main Ave. Gardener
IT John 89 Trist St Roch
Each of the columns are flush, so cutting would work.
Now basically if I enter in "Kare", the script should search the first name column, then spit out "Karen Peters" as the output.
The reason why I want the script to only do partial search in the first name category is in the case the 2nd and 3rd entry, the first and last name are identical respectively.
Thanks for the help!
---------- Post updated at 08:08 PM ---------- Previous update was at 06:31 PM ----------
Actually I think I just figured it out...but I'm still stuck on isolating a GREP search within column parameters. The syntax is probably very easy, I just seem to not be able to find it.
I would want to search a partial name within columns 12-26
So what Ive decide to do is make a while loop, that will go line by line in the database file, and then spits out the first(col 12-26) and last(col 44-58) name as the output
Does this work? #!/bin/bash
read -p "Enter search string : " S
while read L
do
Name1=${L:11:14}
[[ "$S" =~ "$Name1" ]] && echo "Name: $Name1 ${L:43:15}"
# If the above doesn't work
# grep "$S" <<< "Name1" && echo "Name: $Name1 ${L:43:15}"
done <playerlist
I did paste that in a dummy script and renamed it accordingly but it didnt output anything.
The script did run to the user input stage and then concluded. I am trying to understand your code as well I get everything except that I cant seem to find where the grep search would go into the file 'playlist' to hunt down the user input.
Let me know if you'd like me to clarify anything, sometimes I can be vague. thank you for your time
I can try explaining again. So user inputs a name(can be full or partial). The script has to check whether the name exists in the database file(playerlist). Once the script finds a match, it should output the first name and last name columns (12-26, 44-58), however it should check every single line for duplicate first names. So if Michael appears twice, there should be two lines with both Michaels as outputs. I figured a while loop would be appropriate. I will try to play with the code you gave in the meantime though
---------- Post updated at 03:21 PM ---------- Previous update was at 03:05 PM ----------
here's what I was thinking for the while loop
rowCount=$(wc -l <playerlist) #this would store the number of lines in the db
while [ $rowCount - ne 0 ]
do
some grep function to search line @ rowCount and output if match found
rowCount=$(($rowCount-1))
done
# Im thinking that should output all matches found within the database file
Above code reads each line of playerlist and assigns it to the variable L; then prints the variable on screen, until nothing can be read (EOF is reached).
Running the above code should perform the same as typing cat playerlist
Name1=${L:11:14} extracts 14 chars from the 12th char of $L
Then use the following syntax (alternative proposed in my first post):read -p "Enter search string : " S
while read L
Name1=${L:11:14}
grep "$S" <<< "Name1" && echo "Name: $Name1 ${L:43:15}"
done <playerlist