Question about partial searching

Hi there!

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.

This is within Bash.

Any ideas would be greatly appreciated. Thank you :slight_smile:

Post your input file and desire output.


read -p "Enter search string : " S
while read L
do
[[ "$S" =~ "${L::10}" ]] && echo "Name: ${L::10} - Address: ${L:20:10}"
# If the above doesn't work
# grep "$S" <<< "${L::10}" && echo "Name: ${L::10} - Address: ${L:20:10}"
done <playerlist

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.

But how are the columns delimited?
Karen seems to be in the 2nd column, on what part of the line should the search occur?

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 :slight_smile: 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. :slight_smile: 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 :slight_smile:

---------- 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

You could make the list of patterns into a sed script to go through once and print out what matches any, and even with the first it matches:

 
sed '
  /pattern1/b
  /pattern2/b
  .
  .
  .
  /patternN/b
  d
 '

or:

 
sed '
  s/.*pattern1/pattern1: &/
  t
  s/.*pattern2/pattern2: &/
  t
  .
  .
  .
  s/.*patternN/patternN: &/
  t
  d
 '

No need to count the lines:

while read L
do
   echo "$L"
done <playerlist

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

1 Like

ahh okay I understand now!

I ran the script and got this

./test: line 5: conditional binary operator expected
./test: line 5: syntax error near `=~"Name1"'
./test: line 5: `      [[ "$S" =~"Name1" ]] && echo "Name: $Name1 ${L:43:15}"'

=~ is a BASH-ism and won't work in other shells. Even in BASH, I don't think it works the way you have it. What are you trying to do?

that particular part is supposed to check if the user input equals or partially equals $Name1

"partially" equals? What does that mean?

sorry, I should've clarified with an example

so lets say theres a few names in a list

whether the user enters it eter, Pete, ter

it should output Peter as a match. A partial or full match basically

Ah, you mean substring.

In BASH and KSH you can do matching like

if [[ "$HAYSTACK" == *"${NEEDLE}"* ]]

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

that code seems to terminate after I enter in the string that I want to search

Which?

the one Frans was referring to :slight_smile: