esham
March 14, 2005, 7:51pm
1
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
Ygor
March 14, 2005, 9:27pm
2
$ 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 ??
esham
March 15, 2005, 4:43am
4
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
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
esham
March 15, 2005, 7:11am
8
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
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
esham
March 15, 2005, 12:14pm
10
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
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'
tanku
March 16, 2005, 2:52am
13
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
esham
March 16, 2005, 4:06am
14
also please explain the usage : hai
worked grea..thanks..
Is there any way to make changes in the same file...
thanks again
esham
ZealeS
March 16, 2005, 8:17am
15
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.
tanku
March 17, 2005, 12:57am
17
just want to know what is the meaning of power hai? its killing me
tanku
March 17, 2005, 12:59am
18
will it not insert a space at the begining of power hai if hai start in the begining of a line
esham
March 17, 2005, 4:23am
19
that solved my problem....
thanks a lot
it will be helpfull if you explain the usage
esham
ZealeS
March 17, 2005, 5:44am
20
\<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