Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls | grep 'switches'

Status
Not open for further replies.

duncdude

Programmer
Jul 28, 2003
1,979
GB
I am getting into this UNIX stuff and i'm loving it - BUT it's a complex beast

Could someone please tell me if this is the most compact ls command I can write in the terminal to display all the files with the word the in the text:-

[tt]ls | grep -d skip --binary-files=without-match 'the' *[/tt]

This is probably a VERY simple question for you UNIX buffs - but you have to trust me when I say that I am truly overwhelmed by the number of switches available... and their awesome flexibility when piped to other commands. Unfortunately it makes learning the syntax quite difficult. I've experienced seasoned UNIX professionals slipping up...

Please help! :)


Kind Regards
Duncan
 
man grep!

to match all files with the word the:
ls | grep the

to match all with the word the:
ls | grep -v the



___________________________________
[morse]--... ...--[/morse], Eric.
 
Do you mean files with "the" inside them, or in their filename?

filename: ls *the*

inside: grep -Il the * # no ls needed



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
nawlej ... what on earth do you think i've been doing!? :)
Your script returns a vast array of matches that seem to have nothing to do with the word the?

Rod your script returns many operation not permitted's and not one match?

Thank you both for your help with this


Kind Regards
Duncan
 
Duncan, I didn't post a script. Just two commands, with indicators of which one to use depending on your answer to my question.

If you can answer the question, I'll post a much clearer answer.

Do you mean files with the text "the" occurring in the actual contents of the file, or files with "the" in their filename?



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Sorry Rod

Thanks for your reply. I need to find files with 'the' inside the files - NOT the filename


Kind Regards
Duncan
 
I cant even get that command you wrote to work....try this, its not compact, but it works (in Solaris).
Code:
for i in `ls -aF | grep -v "/"`
 do
   FLC=`grep 'the' $i | wc -l`
   if [ "$FLC" -eq 0 ]
     then 
       echo $i
   fi
 done

___________________________________
[morse]--... ...--[/morse], Eric.
 
Code:
grep -m1 -d skip -l -I the *
seems to work.
-m1 (only look for first match)
-d skip (skip subdirs)
-l (only print filenames, not the match)
-I (Ignore binaries)

2 minutes grep --help
Of course your grep might differ...

seeking a job as java-programmer in Berlin:
 
I've only just read that numeric comparisons can involve > /dev/null ... what???

I get alot of replies that incinuate that it's only going to take 2 minutes to learn UNIX - come on... it's a flipping nightmare!!! ;-)


Kind Regards
Duncan
 
Hi Stefan

Thanks for your reply

Unfortunately...

[tt]grep: invalid option -- m
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.[/tt]


I'm using Mac OSX 10.3.4 Panther - in case that helps!?


Kind Regards
Duncan
 
no - seems not to help, since I use linux 2.6.7, and gnu-grep 2.5.1 - in case that helps!?

I don't know how easy it is, to install gnu-grep for MacOsX.

On Windows, you could open the 'Search-Wizzard', insert the directory into the textfield, specify file-type: MS-Notepad-Textfile, ... about nightmares...

grep --help could tell me what options YOU have.

Do you have find?



seeking a job as java-programmer in Berlin:
 
And what about this ?
grep -l 'the' *

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi Stefan

[tt]Duncan-Carrs-Computer:~/Desktop/ab duncancarr$ grep --help
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression
-e, --regexp=PATTERN use PATTERN as a regular expression
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline

Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version print version information and exit
--help display this help and exit
--mmap use memory-mapped input if possible

Output control:
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
-H, --with-filename print the filename for each match
-h, --no-filename suppress the prefixing filename on output
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE
TYPE is 'binary', 'text', or 'without-match'.
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories
ACTION is 'read', 'recurse', or 'skip'.
-r, --recursive equivalent to --directories=recurse.
-L, --files-without-match only print FILE names containing no match
-l, --files-with-matches only print FILE names containing matches
-c, --count only print a count of matching lines per FILE
-Z, --null print 0 byte after FILE name

Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context[=NUM] print NUM (default 2) lines of output context
unless overridden by -A or -B
-NUM same as --context=NUM
-U, --binary do not strip CR characters at EOL (MSDOS)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'. `fgrep' means `grep -F'.
With no FILE, or when FILE is -, read standard input. If less than
two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,
and 2 if trouble.

Report bugs to <bug-gnu-utils@gnu.org>.
Duncan-Carrs-Computer:~/Desktop/ab duncancarr$[/tt]


Kind Regards
Duncan
 
Dunc, if what you're trying to do is print the names of files that have the whole word "the" in them, this does it for me:
Code:
[b]grep -w -l --binary-files=without-match 'the' * | less[/b]
I'm piping the output to less, which is a Linux pager, so it won't scroll off the screen. You may need more there instead.

I don't work on *nix currently, but did for about 10 years.
I've got cygwin (*nix emulation) on my Windows box.

What's this about numeric comparisons involving /dev/null?
Are you sure that's not redirection instead of comparison?

HTH
 
guggach - thanks for your response. am I doing something wrong? i've seen examples of this all over the web
i'm trying to find all the files in a directory with the word 'the' contained in them

mike - thanks for your response also. weird one this, it gave me a few operation not permitted errors and then threw me into vi once i touched a key on the keyboard. i tried it again - same thing


Kind Regards
Duncan
 
Dunc, this is a permissions issue. You don't have read permission on the files you're trying to access. The owner of the files or the superuser will need to change the permissions.
 
This works for me using the berkely windows port of grep:
Code:
grep -w -l --binary-files=without-match 'the' *

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
solaris:

man fgrep

with the:

Code:
fgrep -l the *

without the:

Code:
fgrep -lv the all



___________________________________
[morse]--... ...--[/morse], Eric.
 
well the only difference between the versions seems to be the '-m1' (stop file processing after first match) - I inserted it for performance, but perhaps the '-l' (print only filenames with matches) leads to the same behaviour.
Code:
grep -d skip -l -I the *
should do it.
with
Code:
grep -d skip -l -I the * 2 >/dev/null
you should get rid of annyoing informations of missing permissions.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top