Sunday, January 6, 2013

How to use wc and grep (linux) to find certain strings in a file?

Q. Hi all, First off I have a file called Proxemities.

I know how to use grep to find specific strings like to find the word "test" which looks like
grep -r test /Proxemities

But my question is, does anyone know how to find
"4-letter words"
"1-syllable words with exactly one vowell"
"2-syllable words with exactly two vowels"

A. The first one is easy:

grep \\\<[[:alpha:]]\\{4\\}\\\> file.txt

(Obviously replace "file.txt" with the name of your file)

A little explanation: The first character '\<' matches the beginning of a word, separated by whitespace or beginning of a line. Since you're typing on the command line, you need to '\' escape both the '\' and the '<' characters. [[:alpha:]] matches any alphabetic character. The '\\{4\\}' tells it to match 4 of these. And the '\\\>' matches the end of a word, much like '\\\<' matches the beginning.

There are all kinds of things you can do with Regular Expressions. Do a "man grep" to see all of its features. This works for GNU grep. Other greps may be different.

The syllables will be harder to deal with. I can't think of a way off-hand to do this. You can match any vowel by using the bracket structure [aeiou]. You'd most likely need a more intelligent algorithm to break words down into syllables. Python or PERL would probably be a good bet there. Maybe someone has already written something to break words into syllables.

Hope this helps.

-PHIL

How do find all files not in a certain group using the file command in Linux?
Q. I know that using the find command switch -group (name) I can find all files belonging to that group, but I can't figure out how to find all files not belonging to a certain group. Can anyone help?

A. Put a \! before the -group option.
find <directory> \! -group <group>

How do you find files in your current directory modified more than 48 hours ago in Linux?
Q.

A. You can use the mtime option with find command:
mtime +30 --> file modified 30 days ago.
mtime -30 --> less than 30 days.
mtime 60 --> means exactly 60 days.
Example:
$ find /home/you -mtime -2 -print




Powered by Yahoo! Answers

No comments:

Post a Comment