# Match String and get line number and filename

**URL:** <https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710>\
**Category:** Shell Programming and Scripting\
**Created:** [December 17, 2008, 6:13am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710 "2008-12-17T06:13:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [December 17, 2008, 6:13am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/1 "2008-12-17T06:13:53Z")

</div>

Hi All,  
I'm new to unix shell scripting.. Could someone guide me.

I have to search a string in the entire directory, once the search string is matched, it should print the line number of the string that matches and also the line and along with  
it, it should print the file name.

Thanks,  
Thenz

---

<div class="post-metadata">

**Author:** ![Leion](https://community.unix.com/user_avatar/community.unix.com/leion/32/4676_2.png) [@Leion](https://community.unix.com/u/Leion)\
**Post date:** [December 17, 2008, 6:24am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/2 "2008-12-17T06:24:58Z")

</div>

you may try this:

```nohighlight
grep -n STRING_TO_SEARCH *

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [December 17, 2008, 7:04am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/3 "2008-12-17T07:04:12Z")

</div>

Hi,  
Thanks for that code.  
I tried the code but that is taking a longer time and i'm not getting anything.

I also tried this code, which is working but i wanted only to get the string and give me the line number and filename

#1/bin/sh  
echo $PWD  
echo "enter the string to search :"  
read string  
echo "enter the directory from the search to begin :"  
read dir  
find $dir -type f -exec grep $string {} \;

I'm not sure how to proceed to get the line number and filename.

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [December 17, 2008, 7:18am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/4 "2008-12-17T07:18:30Z")

</div>

below code search all the txt file in current folder, and any line match "pattern" will be printed together with the filename and line number

```nohighlight
@files=glob("*.txt");
sub find{
	my $file=shift;
	open FH,"<$file";
	while(<FH>){
		print $file."->line ".$..":".$_ if (m/pattern/);
	}
	close FH;
}
foreach(@files){
	find($_);
}

```

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/5_5575768a8748004e209b776fc1b2916d.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [December 17, 2008, 7:31am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/5 "2008-12-17T07:31:23Z")

</div>

> [@thenz](#):
>
> find $dir -type f -exec grep $string {} \;

You were almost there. First, add the filename:

```plaintext
find $dir -type f -exec grep $string {} \; -print

```

Then - you got that explained already - use "grep -n" to get the line numbers:

```plaintext
find $dir -type f -exec grep -n $string {} \; -print

```

I suggest reading a man page now and then if you encounter a program or a program call you don't understand fully, like the "-n" switch in "grep". You could have looked up "-n" on the man page of grep and find out this way what its purpose is. This is generally a fruititious approach.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [December 22, 2008, 4:17am UTC](https://community.unix.com/t/match-string-and-get-line-number-and-filename/224710/6 "2008-12-22T04:17:27Z")

</div>

Hi All,

Thanks for all your replies and suggestion, I getting to what i wanted..  
I have few questions which i would like to get clarified. I'm not sure how this function works..

@files=glob("\*.txt");  
sub find{  
my $file=shift;  
open FH,"\<$file";  
while(\<FH\>){  
print $file."-\>line ".$..":".$\_ if (m/pattern/);  
}  
close FH;  
}  
foreach(@files){  
find($\_);  
}

Please explain me about " @files and open FH and foreach(@files){ find($\_); } "

Thanks once again 🙂
