search for files with pattern [A-Z] in unix - linux

I want to search files matching with the pattern [A-Z].
I created some files like test.txt,MANUAL.txt,etc in a directory. Now, I want to filter the files according to the search pattern [A-Z]
Example: suppose I have a file which has following content :
A SCRAPER sITe is a WEBsite that features content123 stolen from other sites and presented as original.
I want my search results matching the pattern [A-Z].
EDIT: only "SCRAPER" should show up in the search results

If you're looking for uppercase words in a file:
grep -o '\<[[:upper:]]\+\>' file
If you're looking for which files in this directory contain an uppercase word:
grep -lo '\<[[:upper:]]\+\>' *

You can use grep like this:
grep '[A-Z]' *
to search all lines matching any upper case letter.
EDIT: In order to search lines only with uppercase letters use:
egrep '^[A-Z]+$' file
OR to grep only uppercase words:
grep -Po '\b[A-Z]+\b' file

Related

List each file that doesn't match a pattern recursively

Tried the following command, it lists all the lines including file names
which are not matching the given pattern.
grep -nrv "^type.* = .*"
"But what we need is list of file names in a folder with content
which does not have even a single occurrence of above pattern."
Your help will be really appreciated.
You need the -L option:
grep -rL '^type.* = .*' directory_name
From the GNU grep manual:
-L, - -files-without-match
    Suppress normal output; instead print the name of each input file from which no output    would normally have been printed. The scanning will stop on the first match.

Find the words do not appear in the dictionary in linux platform

Here's the text maybe have some words ,each line has one word.and I accept it as a command line arguments. For example the textile a.txt is like:
about
catb
west
eastren
And what I want to do is to find the words do not in the dictionary, if the words are dictionary words, delete it in the textfile.
I use the following commands:
word=$1
grep "$1$" /usr/share/dict/linux.words -q
for word in $(<a.txt)
do
if [ $word -eq 0 ]
then
sed '/$word/d'
fi
done
Nothing Happened.
grep alone is enough from what I understand
$ grep -xvFf /usr/share/dict/linux.words a.txt
catb
eastren
catb and eastren are words not found in /usr/share/dict/linux.words. The options used are
-x, --line-regexp
Select only those matches that exactly match the whole line. For a regular expression pattern, this is
like parenthesizing the pattern and then surrounding it with ^ and $.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines,
any of which is to be matched.
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the
-e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and
therefore matches nothing.
An alternative would be to use a spell checker like hunspell, so you can apply it to any text, not only a pre-formatted file with only one word per line. You can also specify several dictionaries, and it shows only words which are in none of them.
For example, after copy/pasting the content of this page into test.txt
lang1=en_US
lang2=fr_FR
hunspell -d $lang1,$lang2 -l test.txt | sort -u
produces a list of 46 words:
'
Apps
Arqade
catb
'cba'
Cersei
ceving
Drupal
eastren
...
WordPress
Worldbuilding
xvFf
xz
Yodeya

How to grep specific *name* in subfolders in Linux?

To grep some string in all subfolders I can use:
grep -rl "test" /path
I would like to grep some specific string assuming that the first part and the second part of the string could be different (has got different begin and end) and is case insensitive.
I tried to do something like this, but it doesn't work:
grep -rli "*test*" /path
Could you please tell me how to do this?
To be clear I would like to find all files (grep) including subfolders, where there is specific string, like e.g.:
"*test*"
You should try grep accepting perl regex:
grep -irlP ".*test.*" /path
The .* means matching (zero or more times) any characters except newline. More about meta-characters here
You can use almost any regex, so it's a really flexible solution to match complex patterns. For instance, if you searched for entire words only:
grep -rlP "\b(test)\b" /path
Of course there are limitations. For instance, multiline search with this method can be difficult.
You can try like this using the "--include" option:
grep -rli --include="test*" /path
From the man page of grep
--include=GLOB
Search only files whose base name matches GLOB (using wildcard
matching as described under --exclude).
grep -ilr "string" path/to/subdir
Will search sub-strings as well. grep searches pattern - you don't need those asterisks (or dot asterisks as in my pre-edit answer).
If you want to search the exact word and not its presence in sub-strings, use -w option.

Using grep to find string in two files?

I would like to find a particular string in two text files - I know how to do it in a single file, but how do I select more than one file, the example below does it for a single text file, I want to search file myfile.txt and otherfile.txt
grep "redeem" /home/tom/myfile.txt
You can do like this,
grep 'redeem' file1 file2 file3..
Syntax:
grep [OPTIONS] PATTERN [FILE...]
Normally, grep can do the pattern matching in more than one files.

How to use grep with regex to find all the files containing a 'String'?

I am using SSH secure shell in Unix, I want to search file with some 'TEXT' or string
and that string is like 'PACKAGE', followed by '%PA%EVENTS%'('PA_PACKAGE %PA%EVENTS%')
this % i am assuming as in SQL*PLUS, means i a string PA can occur any where before EVENTS
and EVENTS can occur any where after PA,
I want to search those file which this REGEX matches.
I ma not sure exactly what you are hoping to match, but the following will print out the lines that contain the text "PACKAGE" followed but zero or more characters and then the text "EVENTS", from all the files in the current directory.
grep "PACKAGE.*EVENTS" *
Please give some more examples of what you want, so I can be more specific.
grep -l "PACKAGE*EVENTS" *
recursive:
grep -Rl "PACKAGE*EVENTS" *
try this: egrep -e "PACKAGE PA*EVENTS" file

Resources