Hello All,
I have a file say
cat test.txt
sathya sxk opt
Sandy sxr omg
Ram rxm opt
Now I would like to change opt to omr only for the line in my file
sathya sxk opt
How can i do it.
I know we can do it with sed -i option, but its not present in my system, please help me some solution.
perl -i.bak -pe 's/opt/omr/ if /sathya/' test.txt
awk '/sathya.*opt/{gsub("opt","omr")}1' input.txt > output.txt
ah thanks for the Quick response.
@Elixir: can you please tel me what is -i.bak
@itkamaraj : Here I want to make the changes in the same file. input.txt, output.txt will not work out for me. so can I accomplish it.
-i.bak (inline edit) is for taking backup of your orginal file and apply the changes in your original file itself.
# cat test.txt
sathya sxk 123
Sandy sxr omg
Ram rxm opt
# perl -i.bak -pe 's/123/S@THYA01/ if /sathya/' test.txt
# cat test.txt
sathya sxk S
Sandy sxr omg
Ram rxm opt
Will this not work Properly if special characters are in place ?
May be we have to give \ before that.
JFYI: I will be using variables in my Script for 123 and S@THYA01, so do I have to make any changes in this PERL command for that ?
or perl -i.bak -pe 's/$var1/$var2/ if /$name/' test.txt will work without any changes to it.
when you are using @, perl think it is an array.
why dont and let us know, as you suggested.
Even I make the values into variables, the data after @ is skipped. Please help.
Hello Friends,
The above PERL works only for Alphabets and not for Alphanumeric or with Special characters.
Since that will be a Password column, User can provide any Character sequence. Please suggest some other Solution.
Try this:
perl -e '$exists=shift @ARGV;$from=shift @ARGV;$to=shift @ARGV;
$fromlen=length($from);$tolen=length($to);
$^I=".bak";
while(<>) {
if(index($_,$exists) != -1) {
while(($s=index($_,$from,$nxt)) != -1) {
substr($_,$s,$fromlen,$to);
$nxt=$s+$tolen;
}
}
print;
}' 'sathya' '123' 'S@THYA01' infile
The first argument ( sathya
in this case) is the "string" which should be present in the line to attempt substitution.
The second argument ( 123
here) is the "string" you want to search for.
The third argument ( S@THYA01
here) is the replacement "string".
The fourth argument ( infile
here) is the file to be changed.
Backup of the original file will be taken with .bak
extension.
1 Like
Many thanks Knight. How can we include this in it
if /sathya/'
find . -type f -print | xargs perl -p -i -e "s/pattern search string/replace string /g"
I've edited my earlier post. Check it now.
Thanks Knight, I will check it.
@Saud Pasha: I should have to specify my file name in the find, isn't it ?
find . -name "filename" -type f | xargs perl -p -i -e "s/pattern search string
/replace string /g"
also not only that, I have to find out a pattern "sathya" and only after that I have to replace the value of its 3rd column.
---------- Post updated 10-16-12 at 02:00 AM ---------- Previous update was 10-15-12 at 03:41 AM ----------
Thanks Knight , it works
---------- Post updated at 02:42 AM ---------- Previous update was at 02:00 AM ----------
Why the below is not working ?
My file is
>> cat test.txt
sathya yuiop New
ramesh sdgh 456
santhosh sdghsdf 789
I just want to try how this works with the awk as said by itkamaraj.
#!/bin/sh
var1=sathya
pwd=New
npwd=S@thya01
echo $pwd $npwd | awk -v var=$var1 '/var.*$1/{gsub("$1","$2")}1' test.txt > temp.txt
cat temp.txt > test.txt
rm temp.txt
After executing the above, my file contents remains the same.
But if I execute the awk statement without variables, it works as expected.
Please advise me on what am i missing.