# Grep to find an exact string

**URL:** <https://community.unix.com/t/grep-to-find-an-exact-string/365804>\
**Category:** Shell Programming and Scripting\
**Created:** [March 27, 2017, 12:28pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804 "2017-03-27T12:28:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [March 27, 2017, 12:28pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/1 "2017-03-27T12:28:07Z")

</div>

Hi all,

I tried searching the forum for this, and I read numerous suggestions here and even on other forums, and I cannot get this to want the way that I need it to. I tried grep -W / -f to no luck. Here is what I have. I have a list of file names-

```nohighlight
  
 FILE1-FILE1TEST,FILE1RELATION
 FILE1-FILE1TESTBACKUP,FILE1TESTT
 1234-FILE1TEST,FILE1234
  
 

```

I want to return only lines that contain FILE1-FILE1TEST exactly, not the other two hits, which for this test, I guess they can be considered, old data, or "versioned" data. here is what I have tried:

```nohighlight
grep -F 'FILE1-FILE1TEST' output.out
grep "\FILE1-FILE1TEST\b" output.out
grep '^[^#]*FILE1-FILE1TEST' output.out
grep -w "FILE1-FILE1TEST" output.out
grep "\<FILE1-FILE1TEST\>" output.out  
 

```

Any hints? I have a feeling grep is the right way to approach this but I could be incorrect. thanks

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 27, 2017, 12:40pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/2 "2017-03-27T12:40:45Z")

</div>

```nohighlight
grep "^FILE1-FILE1TEST," filename

```

Another way to approach this would be awk, which understands the concepts of seperators and fields, so you can say "Print when field one matches this exact text", which seems to me a nicer way to write it but YMMV. Both work.

```nohighlight
awk -F, '$1 == V' V="FILE1-FILE1TEST" inputfile

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 27, 2017, 1:00pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/3 "2017-03-27T13:00:12Z")

</div>

What exactly do you dislike with `grep` s 2, 4, and 5? They all yield `FILE1-FILE1TEST,FILE1RELATION`

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [March 27, 2017, 2:47pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/4 "2017-03-27T14:47:59Z")

</div>

IMO you need to supply a suitable anchor for the pattern you are looking for in order to filter out lines that are similar but don't match your criteria...

```nohighlight
grep '^FILE1-FILE1TEST[^a-zA-Z]' output.out

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 27, 2017, 6:09pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/5 "2017-03-27T18:09:31Z")

</div>

What is your OS and version?

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [March 27, 2017, 9:47pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/6 "2017-03-27T21:47:54Z")

</div>

This is being done on AIX 7.1

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 27, 2017, 9:58pm UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/7 "2017-03-27T21:58:41Z")

</div>

Do you only want to find the string you're searching for at the start of a line? Or do you want to display a line if the string you're searching for appears at the end of a line?

Do all lines have two fields (as in your example)? Or, could some lines have only one field. Could some lines three or more fields?

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 28, 2017, 12:38am UTC](https://community.unix.com/t/grep-to-find-an-exact-string/365804/8 "2017-03-28T00:38:37Z")

</div>

grep on AIX does not have word boundary operator extensions (GNU,BSD) like `\<` , `\>` or `\b` , so they will not work.  
It does however have the -w extension so I do not understand why

```nohighlight
grep -w "FILE1-FILE1TEST" output.out

```

would not work.

Anyway, you could try this, which should work with any (POSIX compliant) grep:

```nohighlight
grep -E '(^|[^[:alnum:]_])FILE1-FILE1TEST([^[:alnum:]_]|$)' output.out

```

--  
Note that the data sample has a leading space on every line..
