Exact expression matches (can't seem to solve this)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exact expression matches (can't seem to solve this)
# 1  
Old 12-13-2012
Exact expression matches (can't seem to solve this)

I've seen dozens of similar threads but none seem to match what I'm looking for and I can't seem to make sense of how to do this so any help would be immensely appreciated.

I am running a command that generates this output:
Code:
Mike Smith
Mike Smith Alaska
Mike Smith Washington
Mike Smith Alaska Common
Mike Smith Washington State

I am trying to just grep "Mike Smith" but I get a match with all the other lines that contain those characters in addition to other words

I've tried variations of grep -x -w -o, etc without success.

I have also tried sed but I must confess that I do not understand the language enough to formulate a command that will give me what I need.

Does anyone have any suggestions on how I might be able to accomplish this?

Thank you
Mike

on edit: I am not just trying to grep the shortest line or just the name. At some point I might need to grep a different line that has various matches too. I just need to know how to grep exactly what I input...no more and no less.

Last edited by Franklin52; 12-14-2012 at 03:23 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 12-14-2012
Use awk:-
Code:
awk '$0=="Mike Smith"' filename

# 3  
Old 12-14-2012
If you want to get what is exactly in a line of text

Code:
grep '^Mike Smith$' somefile

This means: precisely at the start of the line (^) find the string "Mike Smith" and "$" means that the end of the line is where the "$" is. No extra spaces or tabs.
# 4  
Old 12-14-2012
Thanks for the replies These are the results:

===========================================
Code:
[root@eltreum tmp]# cat file.tmp
Mike Smith
Mike Smith Alaska
[root@eltreum tmp]# cat file.tmp|awk '$0=="Mike Smith"'
[root@eltreum tmp]#

===========================================
Code:
[root@eltreum tmp]# cat file.tmp |grep '^Mike Smith$'
[root@eltreum tmp]#

===========================================

can't seem to get it.

Last edited by Franklin52; 12-14-2012 at 03:23 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 12-14-2012
There will be control characters in your file, can you post the result of this command:-
Code:
cat -Ev file.tmp

# 6  
Old 12-16-2012
Code:
[root@eltreum tmp]# cat -Ev file.tmp
Mike Smith$
Mike Smith Alaska$


Last edited by Scott; 12-16-2012 at 05:47 AM.. Reason: Code tags
# 7  
Old 12-16-2012
PLs use code tags as advised.
The commands work. So it must be within your file. Pls post output of od -tx1 -a file.tmp.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

3. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

4. 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

5. Shell Programming and Scripting

Changing exact matches with awk from subfields

To give you some context of my issue the following is some sample dummy data. The field delimiter is "<-->". The 4th field is going to be tags for my notes. The tags should always be unique and sorted alphabetically. 1<-->01/20/12<-->01/20/12<-->1st note<-->1st note<-NL->2 lines... (4 Replies)
Discussion started by: adamreiswig
4 Replies

6. Shell Programming and Scripting

regular expression exact match

hi everyone suppose we have two scenario echo ABCD | grep \{4\} DATE echo SYSDATE | grep \{4\} SYSDATE i want to match the string of four length only please help (5 Replies)
Discussion started by: aishsimplesweet
5 Replies

7. Shell Programming and Scripting

How to substitute a line that matches an expression with another line

I need some help. I have a file (all.txt) whereby I want to substitute using sed/awk all lines that matches an expression with another line with different expression i.e subtitute expression, database_id: filename; WITH database_id: PY; There are many occurrences of the expression... (4 Replies)
Discussion started by: aimsoft
4 Replies

8. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

9. Shell Programming and Scripting

Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily. So far I have this: rewrite ^action/static/(+)/$ staticPage.php?pg=$1&%$query_string; What I want done... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies

10. UNIX for Dummies Questions & Answers

Exact match with regular expression

Hi I have a file with data arranged into columns. The first column is the chromosome name. When I use grep to subset only rows with chr1, I get chr1 but also chr10, chr11,.. How do I get only rows with chr1? grep chr1 filein > fileout head fileout chr1 59757841 chr11 108258691 ... (2 Replies)
Discussion started by: jdhahbi
2 Replies
Login or Register to Ask a Question