Grepping for Exact Strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping for Exact Strings
# 22  
Old 04-20-2009
use awk:

awk '{gsub(/\<airplane\>/,"helicopter",$0);print}' filename


cheers,
Devaraj Takhellambam
# 23  
Old 04-20-2009
@quirkasaurus: The pipe is the alternating operator, demanding that either the expression on the left or on the right has to be met. In this particular regex it means that the string "airplane" starts straightaway or after a whitespace character (\s). The whitespace char then, however, would not be in the regex result because of the "?<=" sequence. That is called positive lookbehind meaning that after a whitespace the string "airplane" has to be found.
You've got something similiar at the end of the regex: This time we're dealing with a positiv lookahead assertion, though. Additionally, the pipe operator is there one again, telling that the expression has to be followed by a whitespace char (lookahead) or ends right away ($).

@devtakh: Unfortunately, it's not changing anything. What's the use of the escaped angle brackets?
If you want to go with awk you could use my script in a for loop. Each iteration then sets a new character as input field separator and output field separator.
But hopefully, someone will provide a more elegant solution Smilie
# 24  
Old 04-20-2009
Quote:
Originally Posted by quirkasaurus
rubin -- could you explain the pipe and stuff?
I don't see why you left off the < on the end-of-line expression.
or what the question mark is doing...

Code:
s/(^|(?<=\s))airplane((?=\s)|$)/helicopter/g;

Thanks,
Already explained perfectly by Gunther. The only thing I'd add is that lookahead-lookbehind are zero-width assertions, meaning that they don't match characters, but rather define positions where the pattern is located ( in this example, the pattern is airplane ), same concept as the anchors ^ and $, they define positions but don't match characters per se. It's a powerful and very useful concept.
# 25  
Old 05-04-2009
Quote:
Originally Posted by Gunther
@quirkasaurus: The pipe is the alternating operator, demanding that either the expression on the left or on the right has to be met. In this particular regex it means that the string "airplane" starts straightaway or after a whitespace character (\s). The whitespace char then, however, would not be in the regex result because of the "?<=" sequence. That is called positive lookbehind meaning that after a whitespace the string "airplane" has to be found.
You've got something similiar at the end of the regex: This time we're dealing with a positiv lookahead assertion, though. Additionally, the pipe operator is there one again, telling that the expression has to be followed by a whitespace char (lookahead) or ends right away ($).

@devtakh: Unfortunately, it's not changing anything. What's the use of the escaped angle brackets?
If you want to go with awk you could use my script in a for loop. Each iteration then sets a new character as input field separator and output field separator.

But hopefully, someone will provide a more elegant solution Smilie
The escape angle brackets are suppose to match the word aeroplane and not something like.
example: "airplanewomen, airplaneman, sweetairplane, sourairplane".
May be you should use gawk if awk doesn't work for you.
# 26  
Old 05-04-2009
I tried gawk (3.1.5) but it's still not working.
Here's the output text:
Quote:
my-do-not-change-helicopter and other frogs
helicopter.seriously change me
sometimes in the file i'm using,
there are words like this: db-helicopter, db-12.helicopter.
in cases like that, your code turns
the words into db-helicopter, db-12.helicopter.
helicopter,frogs,somewerirdairplane buggly buggly
aardvark,chameleon,helicopter,dugong,basilisk
aardvark,chameleon,dugong,basilisk,helicopter
Your script is changing every airplane into a helicopter as long it's not really part of a word as in "somewerirdairplane".

You say it's working at your computer? What release of (g)awk are you using? Really strange...
# 27  
Old 05-04-2009
Its a part of GNU awk's extended regular expression...check out yourself
Code:
\<away will match away but not stowaway
stow\> will match stow but not  stowaway

Looks like the requirement changed eventually...if this is not what was intended
# 28  
Old 05-04-2009
my script worked fine and I gave notes on how to modify it in the original post
to work a little tighter, or looser depending on what in the world the original
post actually needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract between two Exact matched strings.

more data.txt i need this exacted from data.txt This is the command i tried sed -n "/Start_of_DISK_info:\/u/,/End_of_DISK_info:\/u/p" data.txtBut, unfortunately it does not do an exact match. Instead, it prints text between both these strings /u & /u/tmp like below. i need this... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Grepping exact pattern and deleting the rows

Hi All, I have the Input as below: 21011513 6030 5570 1710 0 0 5140 0 3430 3430 0 21049513 152340 138260 101210 0 0 134440 0 33880 33880 0 31003514 16100 13280 7580 3250 1530 15650 8090 8100 ... (22 Replies)
Discussion started by: am24
22 Replies

3. Shell Programming and Scripting

Grepping multiple strings from one column

I have 3-column tab separated data that looks like the following: act of+n-a-large+vn-tell-v 0.067427 act_com of+n+n-a-large-manufacturer-n 0.129922 act-act_com-com in+n-j+vn-pass-aux-restate-v 0.364499666667 com nmod+n-j+ns-invader-n 0.527521 act_com-com obj+n-a-j+vd-contribute-v 0.091413... (2 Replies)
Discussion started by: owwow14
2 Replies

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

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

How to extract exact strings in shell scripting

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

7. Shell Programming and Scripting

Grepping Multiple Strings on the Same Line 'Or'

I've got this command that I've been using to find strings on the same line, say I'm doing a search for name: find . -name "*" | xargs grep -i "Doe" | grep -i "John" > output.txt This gives me every line in a file that has John and Doe in it. I'm looking to add a OR operator for the second... (5 Replies)
Discussion started by: Rally_Point
5 Replies

8. Shell Programming and Scripting

Grepping for two strings that MUST exist on the same line

Trying to find a way to grep for two names on a line. Both names must appear on the same line so '|' / OR is out. So far, I'm just messing around and I've got find . -name "*" | xargs grep "Smith" Let me explain. I'm at a top level and need to know all the names of the files that... (6 Replies)
Discussion started by: Rally_Point
6 Replies

9. Shell Programming and Scripting

Trouble grepping for multiple strings

I am having a heck of a time trying to write a script that will grep for multiple strings in a single file. I am really at my wits end here and I am hoping to get some feedback here. Basic information: OS: Solaris 9 Shell: KSH Oracle Database server I was trying to grep through a file... (5 Replies)
Discussion started by: thecoffeeguy
5 Replies

10. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies
Login or Register to Ask a Question