Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find data in file corresponding to a search criteria

Status
Not open for further replies.

mikawin

Technical User
Apr 15, 2009
28
US
Hello,

I have a file with 3 columns. I want to get data corresponding to Col3 based on matching criteria in Col1.

For example my searchInFile.txt contains:
100 P 24
200 Q 31
300 R 65
200 S 41

I need to extract Col3 corresponding to all values that match 200 in Col1.

In Unix, I can do $foundValue = `grep ^$200 | cut -f 3 searchFile.txt` and keep appending to it.

How can I translate this to an equivalent code an in Perl?

Thanks,
Mika
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]searchInFile.txt[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [black][b]my[/b][/black] [blue]$ih[/blue], [blue]$file[/blue] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open file, [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]@matched[/blue] = [red]([/red][red])[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<[blue]$ih[/blue]>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[black][b]my[/b][/black] [blue]@data[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red] eq [red]'[/red][purple]200[/purple][red]'[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@matched[/blue], [blue]$data[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]'[/red][purple],[/purple][red]'[/red], [blue]@matched[/blue][red])[/red] . [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]- Miller
 
I'm not sure what you're doing with the values once you find them, but this might work for you too:
Code:
perl -anF/\s+/ -e "print \"$F[2]\n\" if $F[0] == 200" searchInFile.txt
 
Thank you rharsh for your suggestion. Is it possible to assign the value of this search to an array, since I will be checking the contents of the array for a downstream process.

Thanks,
Mika
 
Take a look at the code MillerH posted, all the matches should be in @matched if you use similar code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top