Match String and get line number and filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match String and get line number and filename
# 1  
Old 12-17-2008
Match String and get line number and filename

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
thenz
# 2  
Old 12-17-2008
you may try this:


Code:
grep -n STRING_TO_SEARCH *

# 3  
Old 12-17-2008
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.
thenz
# 4  
Old 12-17-2008
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

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

# 5  
Old 12-17-2008
Quote:
Originally Posted by thenz
find $dir -type f -exec grep $string {} \;
You were almost there. First, add the filename:

Code:
find $dir -type f -exec grep $string {} \; -print

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

Code:
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
# 6  
Old 12-22-2008
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 :-)
thenz
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Get remaining line after string match

Want to get the remaining line after pattern match Here it starts - executed commands : - pattern to identify 100:27:500:1:34:END Required output:100:27:500:1:34:END awk '{if(/pattern to identify/) print $2}' < file I have used above code and it not giving... (3 Replies)
Discussion started by: rozee
3 Replies

3. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

5. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

6. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

7. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

8. Shell Programming and Scripting

Getting filename for Nth line pattern match

Hi, I have many scripts in particular directory. And few of the scripts have exit 0 in second line. Now i wanted to list out the scripts name which has the exit 0 in its second line I tried many options , but i can not get the filename along with the nth line pattern match :mad:. Can anyone... (14 Replies)
Discussion started by: puni
14 Replies

9. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

10. UNIX for Dummies Questions & Answers

Get Filename and Line Number using grep

Hi, I am using the Korne shell to try and get the filename, line number and the line of text using grep e.g. find ./ -type f -name "*.java" -exec grep -nf test.txt '{}' \; (test.txt contains strings to search) will return the line number and the line of text. grep -l would return the... (4 Replies)
Discussion started by: ceemh3
4 Replies
Login or Register to Ask a Question