The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep help flood Shell Programming and Scripting 3 06-06-2008 02:14 AM
Grep Aejaz UNIX for Advanced & Expert Users 3 04-30-2008 08:10 AM
grep dineshr85 Shell Programming and Scripting 1 10-10-2007 05:52 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 03:59 AM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 01:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-14-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
grep pls help..

Hello,

Iam trying to replace all the occurences of "hai" to all the files recursively and want to replace this by "power hai"

But in the contents there are occurences like $hai, which i dont want to replace.

Can I do the following grep

Quote:
grep '[^A-Za-z]hai[^A-Za-z]' ./
grep '^hai[^A-Za-z]' ./
grep '[^A-Za-z]hai$' ./
but this does not work for me..

Now i have to replace this to power hai..
I hope i can use sed..

Is there any one who can help in this..

Thanks in advance
Esham
  #2 (permalink)  
Old 03-14-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
$ echo 'hai hail $hai thai hai' > file1
$ perl -pi -e 's/(^|[[:space:]])hai($|[[:space:]])/\1power hai\2/g' file1
$ head file1.bak file1
==> file1.bak <==
hai hail $hai thai hai

==> file1 <==
power hai hail $hai thai power hai
  #3 (permalink)  
Old 03-15-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
Quote:
Originally Posted by Ygor
$ echo 'hai hail $hai thai hai' > file1
$ perl -pi -e 's/(^|[[:space:]])hai($|[[:space:]])/\1power hai\2/g' file1
$ head file1.bak file1
==> file1.bak <==
hai hail $hai thai hai

==> file1 <==
power hai hail $hai thai power hai
Its working perfect..
Can any one convert this to bash script.

Thanks
esham
  #4 (permalink)  
Old 03-15-2005
sglinux sglinux is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 6
grep

This one will work, however it prints to new line
sed 's/ /\n/g' abcd.txt | sed 's/^hai/power hai/g'
  #5 (permalink)  
Old 03-16-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
what about

Code:

sed 's/^hai/ hai/g' hai.txt | sed 's/[ ]hai/ power hai/g'
  #6 (permalink)  
Old 03-16-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
Quote:
Originally Posted by bhargav
what about

Code:

sed 's/^hai/ hai/g' hai.txt | sed 's/[ ]hai/ power hai/g'
also please explain the usage : [ ]hai

worked grea..thanks..
Is there any way to make changes in the same file...

thanks again
esham

Last edited by esham; 03-16-2005 at 05:08 AM.. Reason: to add
  #7 (permalink)  
Old 03-16-2005
tanku tanku is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 53
perl is very useful,you should install it!
i like the look back feature. still learning a little regex..

perl -p -i -e 's/\b(?<!\$)hai\b/power hai/g' filename
Closed Thread

Bookmarks

Tags
perl, perl regex, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0