Search tip.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search tip.
# 1  
Old 07-13-2011
Search tip.

How do I find a key word in multiple files.. in a directory.. ?
Code:
cat *.doc | grep -i myword?


Last edited by Franklin52; 07-14-2011 at 03:19 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-13-2011
Code:
grep myword *.txt

Your way would actually work, but wouldn't tell you which file things were found in. Probably not a good habit to use cat all the time anyway.
# 3  
Old 07-13-2011
Code:
cat *.doc |xargs grep -i myword
find . -type f -name *.doc -exec grep -i mywork {} \;

# 4  
Old 07-13-2011
Code:
 
find . -type f -name "*.doc" | xargs grep -i myword

# 5  
Old 07-14-2011
---------- Post updated at 03:56 PM ---------- Previous update was at 03:55 PM ----------

grep -R "myword" *
would do the needful .

With this command you can search the keyword even in the subdirectory files also .
# 6  
Old 07-14-2011
Quote:
Originally Posted by hamon
How do I find a key word in multiple files.. in a directory.. ?
Code:
cat *.doc | grep -i myword?

Use ls instead of cat and -l option of grep would tell you which file-name contains the 'myword'
Code:
ls *.doc |xargs grep -li 'myword'

Or try a simple method as
Code:
grep -li 'myword' *.doc

# 7  
Old 07-14-2011
Quote:
Originally Posted by michaelrozar17
Use ls instead of cat and -l option of grep would tell you which file-name contains the 'myword'
That is a useless use of ls *. At best your the result is no different, and less efficient. Worst case, xargs will be confused by filenames with spaces when a simple *.doc would not.

It won't help fight "too many arguments" errors either. Feeding the too many arguments into ls instead of grep is still too many arguments. The way to not get too many arguments is to not use too many arguments, by using tools like find instead of shell globbing.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Tip: inotify cron

Dear members, moderators and others. While working on <insert project>, a need has surfaced to watch a directory, and when a file comes, to do appropriate action. So, i started writing some shell code, aware of linux inotify-tools package with inotifywait. Also, i'm seeing a lot of similar... (1 Reply)
Discussion started by: Peasant
1 Replies

2. Shell Programming and Scripting

[Tip] A better echo

Often it has been said that echo is neither portable nor correct. Here is an input.txt: line1 line2 -n line4 -en line6 -x line8 Then the following fails with BSD/Linux/bash: while IFS= read line do echo "$line" done < input.txt It is elegantly improved by means of an echo... (2 Replies)
Discussion started by: MadeInGermany
2 Replies

3. UNIX for Advanced & Expert Users

Tip: how to get the deepest directories

A couple of times there was the question: in a directory hierarchy how to display the deepest directories only. For example in Help with listing only subfolders. My hot solution at that time was (in short) find . -depth -type d | awk -F / 'NF>=p; {p=NF}' But another article has opened my eyes... (6 Replies)
Discussion started by: MadeInGermany
6 Replies

4. Shell Programming and Scripting

Regexp tip

Hello, I'm just starting working on it. I'd like to get a tip For istance if I have a file like: a b c d e f .... and I wanna get: 1a & 2b & 3c 0d & 8e & 4f ..... I would like to use sed and come up with a regular expression that works.... (3 Replies)
Discussion started by: Dedalus
3 Replies

5. Shell Programming and Scripting

a tip in python commands >> and <<

q = ((i + 2) * 6) >> 3 what's the meaning in python? (3 Replies)
Discussion started by: kazikamuntu
3 Replies

6. Solaris

Solaris; tip

plz explain TIP in solaris in detail. (11 Replies)
Discussion started by: karman0931
11 Replies

7. Shell Programming and Scripting

Little bit of a help or just a tip

I am about to do a script that change the COST so i dont need to change each cost. The output looks like this. "OL_ID OL_LINK_COST ----------- ------------ 51 10 52 10 53 10 54 10 55 ... (3 Replies)
Discussion started by: maskot
3 Replies

8. Solaris

tip into 280R

I need to use tip from machine A serial port to machine B serial port. Can someone point me to an example of the correct cable to use? Thanks. (1 Reply)
Discussion started by: dangral
1 Replies

9. UNIX for Dummies Questions & Answers

one teaching Tip

Student have huge interest about why so many expert choose use UNIX than MS Windows. I consider that SHARE & OPEN is key point.:) (2 Replies)
Discussion started by: 111000
2 Replies
Login or Register to Ask a Question