Search directory and search File

Malarose03

New Member
Credits
43
How can i first search with certain filename with wild card and then search a string in those filename and get the output.

E.g
Directory name : abc/def/xyz/
Files in directory : Abc12, abc13, abd12,abc14,abc15, abe12 and so on.

First search : ab*12
Output : abc12,abd12,abe12

Now search string 'pull' in the output files from first search and display the output with filename and the line where string is present.

I tried with below but no luck :
Ls -lrt Grep 'ab.*12' | xargs grep pull

May be a single command or a series of command that i can put in a script and execute, anything will do as long as the output comes as desired.

Thanks in advance.
 


wizardfromoz

Super Moderator
Staff member
Gold Supporter
Credits
5,868
G'day folks and welcome @Malarose03 :)

Moving this to Command Line, where more responses may be generated.

Cheers

Chris Turner
wizardfromoz
 

wizardfromoz

Super Moderator
Staff member
Gold Supporter
Credits
5,868
Done - good luck :)
 

JasKinasis

Well-Known Member
Credits
2,929
Also, you can use find with grep like this:
Bash:
find ./ -type f -iname "ab*12" -exec grep -RiHn pull {} \;
That will find files with names of any length that start with ab and end in 12 and then invokes grep to search matching files for the string pull.
NOTE:
In the above, the options I've used with grep are:
-R - recursive
-i - case insensitive
-H - show file-name of matched line
-n - show line-number of matched line

If you want the grep search to exactly match "pull" and not "PULL", "pUll" or any other upper/lowercase combination, remove the -i option from the grep invocation. Then it becomes a case-sensitive search.
 


Members online


Latest posts

Top