Grep to find an exact string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep to find an exact string
# 1  
Old 03-27-2017
Grep to find an exact string

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-

Code:
  
 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:

Code:
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
# 2  
Old 03-27-2017
Code:
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.
Code:
awk -F, '$1 == V' V="FILE1-FILE1TEST" inputfile

# 3  
Old 03-27-2017
What exactly do you dislike with greps 2, 4, and 5? They all yield FILE1-FILE1TEST,FILE1RELATION
# 4  
Old 03-27-2017
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...
Code:
grep '^FILE1-FILE1TEST[^a-zA-Z]' output.out

# 5  
Old 03-27-2017
What is your OS and version?
# 6  
Old 03-27-2017
This is being done on AIX 7.1
# 7  
Old 03-27-2017
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

2. Shell Programming and Scripting

Grep exact string from main string

Hi , am getting output file, it sontains the below values. ./hawk_DOM1_FIRST_ENV ./hawk_DOM2_SECOND_ENV ./hawk_DOM3_THIRD_ENV Now I need to grep the word "DOM1_FIRST_ENV","DOM2_SECOND_ENV" like that. I tired with cut -d "_". Its not working with any deleimiter. Can you please help to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

3. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

4. UNIX for Dummies Questions & Answers

How to grep the exact string / word ?

Hi All, I have a text / log file which contains strings like meta777, 77, meta, 777. Now I want to write a script which can detect a string 'meta#777' in a text file & number of occurence of 'meta', number of #, number 7, 77, 777. I'm using grep -e '77' filename but no luck. It is returning... (5 Replies)
Discussion started by: adc22
5 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Shell Programming and Scripting

grep exact string from a file

Hi, I have the following output from a file zone "adm.test.com" { abc test1.db } zone "test.com" { xyz test2.db } zone "1.test.com.sg" { 1abc test3.db } zone "3.test.com.uk" { 1xyz test4.db } (6 Replies)
Discussion started by: vchee
6 Replies

7. Shell Programming and Scripting

How to use grep to get an exact string?

Hi there, I've search this forum and find this problem could have been solved by, grep -ho "num=*" input_data The input_data is, 1\11\num1=100\num2=200\newnum1=220\\@ however, what I have got is , num1=100 num1=220 how to get the exact string, (4 Replies)
Discussion started by: liuzhencc
4 Replies

8. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

hi! i'm trying to get grep to do an exact match for the following pattern but..it's not quite working. I'm not too sure where did I get it wrong. any input is appreciated. echo "$VAR" | grep -q '^test:]name' if ; then printf "test name is not found \n" fi on... (4 Replies)
Discussion started by: jazzaddict
4 Replies

9. UNIX for Dummies Questions & Answers

Find exact string

Hi Guys, File is containing data Col1 Col2 ListPrice List Price Price Average Selling Price - Actual ProjPrice Average Selling Price ProjPrice Launch Price ProjCost Cost Now i need to print col2 on the basis of col1 i.e if i Pass Price it should display only "Average... (7 Replies)
Discussion started by: Swapna173
7 Replies

10. Solaris

Find an exact string

Hii,., Can some one guide me ...how to find a exact word or string using "grep" not a part of some other string. say if i need 321 it should not give me 3210 nor 1321 it should only give me line containing string 321 Thanks and Regards, Joy (2 Replies)
Discussion started by: gr8pals
2 Replies
Login or Register to Ask a Question