Find all accented words (diacriticals) using grep? - grep

I have a large list of words in a text file (one word per line) Some words have accented characters (diacriticals). How can I use grep to display only the lines that contain accented characters?

The best solution I have found, for a larger class of characters ("What words are not pure ASCII?") is using PCRE with -P option:
grep -P "[\x7f-\xff]" filename
This will find UTF-8 and ISO-8859-1(5) (Latin1, win1252, cp850) accented characters alike.

I have a solution. First strip the accents using "iconv" then do a "diff" for lines in the original file:
cat text-file | iconv -f utf8 -t ascii//TRANSLIT > noaccents-file
diff text-file noaccents-file | grep '<'

Related

how to grep a word with only one single capital letter?

The txt file is :
bar
quux
kabe
Ass
sBo
CcdD
FGH
I would like to grep the words with only one capital letter in this example, but when I use "grep [A-Z]", it shows me all words with capital letters.
Could anyone find the "grep" solution here? My expected output is
Ass
sBo
grep '\<[a-z]*[A-Z][a-z]*\>' my.txt
will match lines in the ASCII text file my.txt if they contain at least one word consisting entirely of ASCII letters, exactly one of which is upper case.
You seem to have a text file with each word on its own line.
You may use
grep '^[[:lower:]]*[[:upper:]][[:lower:]]*$' file
See the grep online demo.
The ^ matches the start of string (here, line since grep operates on a line by lin basis by default), then [[:lower:]]* matches 0 or more lowercase letters, then an [[:upper:]] pattern matches any uppercase letter, and then [[:lower:]]* matches 0+ lowercase letters and $ asserts the position at the end of string.
If you need to match a whole line with exactly one uppercase letter you may use
grep '^[^[:upper:]]*[[:upper:]][^[:upper:]]*$' file
The only difference from the pattern above is the [^[:upper:]] bracket expression that matches any char but an uppercase letter. See another grep online demo.
To extract words with a single capital letter inside them you may use word boundaries, as shown in mathguy's answer. With GNU grep, you may also use
grep -o '\b[^[:upper:]]*[[:upper:]][^[:upper:]]*\b' file
grep -o '\b[[:lower:]]*[[:upper:]][[:lower:]]*\b' file
See yet another grep online demo.

Find a word that contain exactly 2 same characters in Linux

In Linux how can I find a word that contain exactly 2 same characters. Can I use grep?
You can use the \w back-reference. What this does is match any two characters that are consecutive. I beleive this only works for [a-z,A-Z,0-9].
The back-reference creates a group from which to check the character against ... Then the \1 checks the current character against the previous character.
file.txt
qwerty
qqwerty
123456
1123456
command
egrep '(\w)\1' file.txt
output
qqwerty
1123456

Getting only grep exact matches

I am trying to grep a file for the exact occurrence of a match, but I get also longer spurious matches:
grep CAT1717O99 myfile.txt -F -w
Output:
CAT1717O99
CAT1717O99.5
I would like to output only the first exactly matching line. Is there any way to get rid of the second line?
Thanks in advance.
Arturo
This is the file 'myfile.txt':
CAT1717O99
CAT1717O99.5
This will do the work for you.
grep -Fx "CAT1717O99" textfile
-F means Fixed
-x mean exact
Use the power of Perl-compatible regular expression (PCRE) and search the matches to the given pattern:
grep -Po "\bCAT1717O99(\s|$)" myfile.txt
(\s|$) - alternative group, ensures matching substring CAT1717O99 if it's followed by whitespace or placed at the end of the line
-P option, allows regular expressions
-o option, prints only matched parts of matching lines
You'll need explicitly request spaces in order to ignore special chars.
grep -E '(^| )CAT1717O99( |$)' myFile.txt
from grep manual :
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

Output alpha/numeric/alphanumeric text with grep

I have a single line of text containing 12k of these blocks separated by a comma:
{"hash":"AbCD1QS","title":"","description":"","width":1920,"height":1080,"size":12345689,"ext":".jpg","animated":false,"prefer_video":false,"looping":false,"datetime":"2009-01-04 02:32:61"}
The "alphanumeric" text next to hash can also be all lowercase, all uppercase, contain no numbers, or contain all numbers.
How do I output just the text next to hash using Grep, and each result to a new line?
grep -Po "(?<=hash\":\")[^\"]*"
-P for using perl regular expressions
(?<=hash\":\") matches string after hash":"
[^\"]* matches any characters til the quote mark
Use a JSON parser for JSON data:
$ json='{"hash":"AbCD1QS","title":"","description":"","width":1920,"height":1080,"size":12345689,"ext":".jpg","animated":false,"prefer_video":false,"looping":false,"datetime":"2009-01-04 02:32:61"}'
$ echo "$json" | jq -r '.["hash"]'
AbCD1QS

Grep: First word in line that begins with ? and ends with?

I'm trying to do a grep command that finds all lines in a file whos first word begins "as" and whos first word also ends with "ng"
How would I go about doing this using grep?
This should just about do it:
$ grep '^as\w*ng\b' file
Regexplanation:
^ # Matches start of the line
as # Matches literal string as
\w # Matches characters in word class
* # Quantifies \w to match either zero or more
ng # Matches literal string ng
\b # Matches word boundary
May have missed the odd corner case.
If you only want to print the words that match and not the whole lines then use the -o option:
$ grep -o '^as\w*ng\b' file
Read man grep for all information on the available options.
I am pretty sure this should work:
grep "^as[a-zA-Z]*ng\b" <filename>
hard to say without seeing samples from the actual input file.
sudo has already covered it well, but I wanted to throw out one more simple one:
grep -i '^as[^ ]*ng\b' <file>
-i to make grep case-insensitive
[^ ]* matches zero or more of any character, except a space
^ finds the 'first character in a line', so you can search for that with:
grep '^as' [file]
\w matches a word character, so \w* would match any number of word characters:
grep '^as\w*' [file]
\b means 'a boundary between a word and whitespace' which you can use to ensure that you're matching the 'ng' letters at the end of the word, instead of just somewhere in the middle:
grep '^as\w*ng\b' [file]
If you choose to omit the [file], simply pipe your files into it:
cat [file] | grep '^as\w*ng\b'
or
echo [some text here] | grep '^as\w*ng\b'
Is that what you're looking for?

Resources