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

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

$ 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

what is your desired output looks like ??

would like to do with shell and not perl..

I want to substitue the word "hai" with "power hai" in all the files and files in all the subfolders..recursively.

But there are words like $hai which i want to keep it as it is..since it defined as a variable..

Please help

Esham

bash-2.05$ echo "hai thai haihai $hai"|sed 's/hai/power hai/g'
power hai tpower hai power haipower hai

But that replaced $hai..

:frowning:

send the output to a file

bash-2.05$ more abcd.txt
hai $hai $haihai thai $thai bhai $bhai

bash-2.05$ cat abcd.txt|sed 's/^hai/power hai/g'
power hai $hai $haihai thai $thai bhai $bhai

Hello..
again some problems..

when abcd.txt contains as:

hai $hai $haihai thai $thai bhai $bhai hai

and when we run the script

Iam getting following output :

The last hai is unchanged :frowning:

please check this

sorry for the last one.

bash-2.05$ more abcd.txt
hai hai $hai hai $haihai thai $thai bhai $bhai hai

bash-2.05$ tr -s ' ' '\n' <abcd.txt|sed '1,$s/^hai/power hai/g';echo
power hai
power hai
$hai
power hai
$haihai
thai
$thai
bhai
$bhai
power hai

Its working perfect..
Can any one convert this to bash script.

Thanks
esham

This one will work, however it prints to new line
sed 's/ /\n/g' abcd.txt | sed 's/^hai/power hai/g'

what about


sed 's/^hai/ hai/g' hai.txt | sed 's/[ ]hai/ power hai/g'

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

also please explain the usage : hai

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

thanks again
esham

bash-2.05$ echo 'hai hail $hai thai hai'
hai hail $hai thai hai
echo 'hai hail $hai thai hai' | sed 's/\([^\$]\)hai/\1power hai/g;s/^hai/power hai/'
power hai power hail $hai tpower hai power hai
#full match#
bash-2.05$ echo 'hai hail $hai thai hai' | sed 's/\([^\$]\)\<hai\>/\1power hai/g;s/^\<hai\>/power hai/'
power hai hail $hai thai power hai

I just made that trick to replace 'hai' by 'power hai'
only if it is preceeded by a space, not by any other char.

just want to know what is the meaning of power hai? its killing me :slight_smile:

will it not insert a space at the begining of power hai if hai start in the begining of a line

that solved my problem....

thanks a lot

it will be helpfull if you explain the usage

esham :slight_smile: :slight_smile: :slight_smile:

:wink: :wink:
\<hai\> means total matching.
^\<hai\> means special position, the "hai" maybe begin of the line.

try following, you'll see....

$ echo "1234" | sed 's/\(12\)\(34\)/\1 \2/g'
$ 12 34
$ echo "1234" | sed 's/\(12\)\(34\)/\2 \1/g'
$ 34 12