Question about partial searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about partial searching
# 1  
Old 10-13-2010
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 Smilie

Last edited by Sagramor; 10-13-2010 at 09:51 PM.. Reason: append information
# 2  
Old 10-14-2010
Post your input file and desire output.
# 3  
Old 10-14-2010
bash code:
  1. read -p "Enter search string : " S
  2. while read L
  3. do
  4.    [[ "$S" =~ "${L::10}" ]] && echo "Name: ${L::10} - Address: ${L:20:10}"
  5.    # If the above doesn't work
  6.    # grep "$S" <<< "${L::10}" && echo "Name: ${L::10} - Address: ${L:20:10}"
  7. done <playerlist
# 4  
Old 10-14-2010
Quote:
Originally Posted by pravin27
Post your input file and desire output.

So lets say I have a file that holds this information

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

Last edited by Scott; 10-16-2010 at 07:41 AM.. Reason: Code tags
# 5  
Old 10-15-2010
But how are the columns delimited?
Karen seems to be in the 2nd column, on what part of the line should the search occur?
# 6  
Old 10-15-2010
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
# 7  
Old 10-15-2010
Does this work?
bash code:
  1. #!/bin/bash
  2. read -p "Enter search string : " S
  3. while read L
  4. do
  5.    Name1=${L:11:14}
  6.    &#91;[ "$S" =~ "$Name1" ]] && echo "Name: $Name1 ${L:43:15}"
  7.    # If the above doesn't work
  8.    # grep "$S" <<< "Name1" && echo "Name: $Name1 ${L:43:15}"
  9. done <playerlist
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Partial Match and Replace

Hi, I have a tab delimited text file like this one. I need to do a partial match of a particular cell and then replace matches with an empty cell. So here is a sample: Smith FordMustang ChevroletCamaro Miller FordFiesta Jones KiaSorrento Davis ChevroletCamaro Johnson ToyotaHighlander I... (4 Replies)
Discussion started by: mikey11415
4 Replies

2. UNIX for Dummies Questions & Answers

How to substitute for the partial match?

Hi I have a question and hope I can get answer here. Thank you in advance. I have two files: file1: aa X bb Y cc Z file2: cc A bb B dd C aa D bb E If the 1st column match in both file1 and file2, the 2nd column in file2 will be replaced by the 2nd column in file1. If there is no... (2 Replies)
Discussion started by: yuejian
2 Replies

3. Shell Programming and Scripting

URL partial matching

I have two files: file 1 http://www.hello.com http://neo.com/peace/development.html, www.japan.com, http://example.com/abc/abc.html http://news.net http://lolz.com/country/list.html,www.telecom.net, www.highlands.net, www.software.com http://example2.com ... (1 Reply)
Discussion started by: csim_mohan
1 Replies

4. Shell Programming and Scripting

Partial retrieve

I have this in log file /var/log/maillog XXX YYY ZZZ :15214 I=:25 AAA BBB CCC I need awk/sed operation on this, so that it retrieves only the first IP. cat /var/log/maillog | sed_operation 55.66.77.88 (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

How to replace partial string

I have a list of strings in file: 10 10 AAA 120 13 BBBBB 23 11 CCCCC 11 32 DDDDDD I want to replace first column of the text such as: 10, 129, 23, 11 with 11, 22, 33, 44. I can do line by line, but just not sure how to replace partial string without... (1 Reply)
Discussion started by: ford99
1 Replies

6. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

7. Shell Programming and Scripting

Partial Delete Lines

Hi, Need to delete line prior to (INSERT/UPDATE/DELETE). In case ' available, then keep that. Pls refet below details. Input ====================== l_s := ' INSERT INTO TEST' l_P PD := ' UPDATE INTO TEST' l_D := ' DELETE INTO TEST' This is test Output... (4 Replies)
Discussion started by: saurabhbaisakhi
4 Replies

8. Shell Programming and Scripting

get partial numbers from a string

Hi Everyone, I have: $val="QQ3_1899_CD4". The output will be: 1899. I did $val =~ /(\d+)/g; the output is 318994, then i use substr to get those 1899. This is not efficient. Is any simple way, like just one line can do? Thanks (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. UNIX for Dummies Questions & Answers

Searching partial columns and returning maximum as output

Hello, I am just getting starting with awk and wondering if anyone could help with the following problem. I have a large file of data, 50,000 rows x 6 columns. I would like to search in blocks of 500 rows for a maximum value in a specific column and compile an output file that prints the... (0 Replies)
Discussion started by: xb_analysis
0 Replies

10. UNIX for Dummies Questions & Answers

searching text question

If I am searching through a text file, and it is setup in hierarchical fashion. ie: Memory: 256 MB DDR Kingston CPU: UltraSparc 800 Mhz can i somehow search for CPU and pull out lines below it as well? Like grep only pulls that one line, i want that line... (4 Replies)
Discussion started by: BG_JrAdmin
4 Replies
Login or Register to Ask a Question