Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grep in a directory

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a directory in which there are lots of logs(*.gz). I have to search for particular text in those logs.
I want to know how to write that script in the terminal.
I tried something but it does not seem to be correct syntax.
i.e


Could someone help me with this?
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link has an explanation on how to look for files where the name matches a certain pattern,
and how to find those files in that pattern that contain a certain string.

By combining the -name and -exec options, you can achieve your requirement.

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually my friend showed wrote a script in the termal and it had for loop with ls , zrep with the search test ...
Can you tell me how to do that in that way...
It looked simple as the one you pointed out

Thanks
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sony Agrawal wrote:Actually my friend showed wrote a script in the termal and it had for loop with ls , zrep with the search test ...
Can you tell me how to do that in that way...
It looked simple as the one you pointed out

Thanks
It would be great if you ask your friend to show the solution again, and to share that info with us.
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can and should directly use the filepattern:

instead of the way over ls, which is possible, but errorprone.

This version will fail whith fancy characters like blanks or newlines in filenames.

In allmost all cases 'ls' hasn't anything to do in scripts.

An even more simple solution will be:

 
Deepak Mahalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is pretty simple.

just type : find ~ -name "*.gz" -exec zcat {} \; | grep -n "hello"

this will find all the files having the .gz extension from the home directory and the it will execute the zcat command on those files. on that result we can do a grep that will return the line numbers of the pattern "hello". This will surely work.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Deepak Mahalingam]the answer is pretty simple.

just type : find ~ -name "*.gz" -exec zcat {} \; | grep -n "hello"

this will find all the files having the .gz extension from the home directory and the it will execute the zcat command on those files. on that result we can do a grep that will return the line numbers of the pattern "hello". This will surely work.[/quote]

I don't quiet understand why you send a solutions 2 months later, which is - pardon - inferior in 5 aspects:

- linenumbers wheren't a requierement
- we searched for Hello (uppercase) all the time
- zgrep was already introduced
- find will search in subdirectories
- ~ will search in the home, not in the current directory
 
Deepak Mahalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stefan: If posting a reply after 2 months is wrong, I have no idea why the discussion is still open and available:)
Anyway, my idea is not to give the exact solution, rather my idea was to give a hint, or idea so that based on the solution provided, the person can build up further.

your comments like 'line numbers were not needed, ~ searched from home, 'etc. were not very mature. Anyway, I however really loved your point that 'find searches the subdirectories also'. Now, that was a really good point that you had there.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Mahalingam wrote: If posting a reply after 2 months is wrong, I have no idea why the discussion is still open and available:)

Because discussions are open in general. Maybe there is some need to come back to an older thread.

Anyway, my idea is not to give the exact solution, rather my idea was to give a hint, or idea so that based on the solution provided, the person can build up further.

Yes, and I would consider that a good habit, if better solutions and solutions for the problem mentioned haven't been posted.

Or if you would have introduced your solution with it's advantage: "In case you're interested in searching the subdirs, consider using find like that ..."

your comments like 'line numbers were not needed, ~ searched from home, 'etc. were not very mature. Anyway, I however really loved your point that 'find searches the subdirectories also'. Now, that was a really good point that you had there .


I'm glad to learn here from your mature comments.
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I'd like you to keep this thread on topic. You have stopped talking about the problem domain and started to comment each other's posting style.
This is not the forum to do that. Back to the subject please.
 
Deepak Mahalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the -maxdepth option in 'find' to restrict the level of search. this will thus search only your current directory and perform the action on the zipped files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic