Match Exact Word Using Grep

Display exact matches only with grep

You need a more specific expression. Try grep " OK$" or grep "[0-9]* OK". You want to choose a pattern that matches what you want, but won't match what you don't want. That pattern will depend upon what your whole file contents might look like.

You can also do: grep -w "OK" which will only match a whole word "OK", such as "1 OK" but won't match "1OK" or "OKFINE".

$ cat test.txt | grep -w "OK"
1 OK
2 OK
4 OK

Grepping for exact words with UNIX

Word boundary matching is an extension to the standard POSIX grep utility. It might be available or not. If you want to search for words portably, I suggest you look into perl instead, where you would use

perl -ne 'print if /\baaa\b/' $EAT_Setup_BJ3/Log.txt

Match exact word using grep

Try this:

grep -E "^test1" file

This states that anything that starts with the word test1 as the first line. Now this does not work if you need to fin this in the middle of line but you werent very specific on this. At least in the example given the ^ will work.

Grep for exact match when there is no more words

Use this expression

grep -i ^word$ file

Explanation:

Find all lines starting with (^) at the end of the line ($).

The -i flag makes the match insensitive, remove it if you want a case sensitive match

How to used grep to match exact words

It looks like you're always matching field 4. So awk would be a better solution, since you can simply do an exact match with the whole field:

for i in "${house[@]}"; do
awk -F'\t' -v i="$i" '$4 == i' metexplore_ID.tsv
done

Don't forget the quotes around ${house[@]}; otherwise elements like L-GLUTAMIC ACID will be treated as two different words to match.

You can also avoid creating the array and looping by loading corrected_inhouse_list.txt directly into an awk array:

awk -F'\t' -v i="$i" '
NR == FNR {houses[$0]++; next}
$4 in houses' corrected_inhouse_list.txt metexplore_ID.tsv

Exact Match of Word using grep

how about an awk solution?

awk -F'|' '$1 == "CHICAGO"{print $2}' file

to only print one output, exit once you have a match, i.e.

awk -F'|' '$1 == "CHICAGO"{print $2; exit}' file

Making that more generic, you can pass in a variable, i.e.

awk -v trgt="CHICAGO" -F'|' '{targ="^" trgt " *$"; if ( $1 ~ targ ) {print $2}}' file

The " *$" regex limits the match to zero or more trailing spaces without any extra chars at the end of the target string. So this will meet your criteria to match skip matching CHICAGO PIT|BULL.

AND this can be further reduced to

awk -v trgt="CHICAGO" -F'|' '{ if ( $1 ~ "^" trgt " *$" ) {print $2}}' file

constructing the regex "in-place" in with the comparison.

So you could use more verbose variable names to "describe" how the regex is being constructed from the input and the regex "wrappers" (as in the 3rd example) OR, you can just combine the input variable with the regex syntax in place. That is just a matter of taste or documentation conventions.

You might want to include a comment to explain you are constructing a regex test that would look like the $1 ~ /^CHICAGO *$/.

IHTH

How to use grep()/gsub() to find exact match

Use word boundary \b which matches a between a word and non-word character,

string = c("apple", "apples", "applez")
grep("\\bapple\\b", string)
[1] 1

OR

Use anchors. ^ Asserts that we are at the start. $ Asserts that we are at the end.

grep("^apple$", string)
[1] 1

You could store the regex inside a variable and then use it like below.

pat <- "\\bapple\\b"
grep(pat, string)
[1] 1
pat <- "^apple$"
grep(pat, string)
[1] 1

Update:

paste("^",pat,"$", sep="")
[1] "^apple$"
string
[1] "apple" "apple:s" "applez"
pat
[1] "apple"
grep(paste("^",pat,"$", sep=""), string)
[1] 1

grep matches exact string or with wildcard

grep uses regex for pattern matching. grep -w 'name1*' would make it match zero or more 1s, so name1 and name11 would match.

If it only matches name1 for you it's because you have a file named name1 in the directory and the unquoted name1* will be interpreted by the shell (globbing). Always use quotes around your arguments that contain special characters. If you use a variable, always put " around it.

To make it match any name starting with name1, make it

grep -w 'name1.*' filename
  • . means "any character"
  • .* means "any character, zero or more times".

If the input comes from some external source where * is used as a wildcard, you need to change that string before calling grep.

Example:

search_str='name1*'
new_search_str="$(sed 's/\*/.*/g' <<< "$search_str")"
grep -w "$new_search_str" filename

Grep exact match with multple strings, no dash

Use -x as it only selects exact matches. -w can match a string if it has non-word constituent characters after the substring. See grep(1) .



Related Topics



Leave a reply



Submit