Finding filename based on filecontent


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding filename based on filecontent
# 1  
Old 05-26-2013
Linux Finding filename based on filecontent

Hi,
I have been trying , to find the filename based on some pattern present inside the file
My command is as follows:
filename=`grep -l 'Pattern' path/*.txt `Its strange that it works some times, but doesn't print anything some times .
But my if test -f $filename is passing all the times.
Is there any other solution to find the filename having specific data.
All the help is appreciated.

Thanks in advance.
# 2  
Old 05-26-2013
If you are positive that your pattern will find only ONE file:
Code:
filename=$(find . -type f -exec grep -l 'Pattern' {}  \; )

# 3  
Old 05-26-2013
Finding filename based on filecontent

Thanks Jim will try it out.!Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding files with newlines in filename

I want to use grep to find files that have newlines in the filename. For example, I have a directory where I create three files: $ touch file1 $ touch "file 2" $ touch "file > with > newlines" $ find . ./file 2 ./file1 ./file?with?newlinesI now want to pipe the find output into grep and... (4 Replies)
Discussion started by: Ralph
4 Replies

2. Shell Programming and Scripting

Finding the part of a filename

Hi, I am writing an ebuild for Gentoo Linux operating system. Writing an ebuild is about Bash scripting where I am a newbie. So, my ebuild must find a part of a specific filename. Such a filaname my look like this: libvclient_release_x64.so.740and I must to find the number at the and of... (18 Replies)
Discussion started by: csanyipal
18 Replies

3. Shell Programming and Scripting

Sorting based on filename

Hello , I have to write a bash script. I will explain the logic based on a scenario. Scenario : Suppose I have few files in a Folder X : FILE_201508.list FILE_201510.list FILE_201507.list abc_201510.csv xyz_201508.csv abc_201507.csv def_201507.csv 1) Now ,... (3 Replies)
Discussion started by: smamrm
3 Replies

4. Shell Programming and Scripting

Finding max number in filename and opening it

Hi, I have files named as energy.dat.1 energy.dat.2 energy.dat.3 ... energy.dat.2342 I would like to find the file with maximum number in the filename (ex. energy.dat.2342) and open it. Would you please share your expertize in writing the script? Thanks in advance. (8 Replies)
Discussion started by: rpd25
8 Replies

5. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

6. Shell Programming and Scripting

Finding files with filename format

hi all, i'm trying to find out how to show files having a particular format. i.e. files o570345.out o5703451.out XX_570345_1.RTF so when i search for files using ls *570345* it shows all three files but actually i don't like to see the second file o5703451.out because 5703451 is... (6 Replies)
Discussion started by: adshocker
6 Replies

7. Shell Programming and Scripting

awk based on filename

i need to use awk to replace string in file. if suppose filename is abcd.dat i need to replace in file california with CA else if file name name is cdef.dat i need to replace in file california with CALI i need to use awk(must) any suggestion how can i do that file structure ... (3 Replies)
Discussion started by: er_zeeshan05
3 Replies

8. Shell Programming and Scripting

finding files with unicode chars in the filename

I'm trying to check-in a repository to svn -- but the import is failing because some files waaaay down deep in some graphics-library folder are using unicode characters in the file name - which are masked using the ls command but picked up when piping output to more: # ls -l 1914* -rwxrwxr-x 1... (2 Replies)
Discussion started by: mshallop
2 Replies

9. UNIX for Dummies Questions & Answers

mv files based on filename

I have a directory of about 30,000 image files. The file names are all yearmonthday.jpg however some of the files have yearmonthday-snapshot.jpg i would like to move all files that contain the phrase -snapshot to their own directory. Any assistance with the proper commands would be much... (1 Reply)
Discussion started by: jdblank
1 Replies

10. Shell Programming and Scripting

If based on filename?

ksh. Hi, I am trying to loop through the files in a directory, and process them one by one where a naming convention is met, this is fine. As part of the looping process I need to check the first two charachters of the filename and if they are AA then perform one action, if they are XX... (13 Replies)
Discussion started by: kshelluser
13 Replies
Login or Register to Ask a Question