bash - Replacing a string with a given text using Linux shell script -


i have file named testfile.it contains information like

methun:x:500:500:comment:/home/methun:bin/bash salahuddin:x:501:500:comment:/home/methun:bin/bash 

now implemented following shell program:

 echo "enter name:"    read username    users='cat /mypractice/myfiles/testfile | awk -f ':' '{print $1}''    user in $users           if [ "$user" == "$username" ];         echo "name found , enter new name change."         read newusername           #need code change text on file --->testfile       fi     done 

now suppose need change methun moin. comment newcomment. used

sed -i 's/"$user"/"$newuser"/g' /mypractice/myfiles/testfile 

but not working here. test in testfile singly change , replace all.but need change position want .

i tried usermod not works here..

can give me solution or correct code...thanks

you using g flag in sed command means global substitution (will change occurrences). also, variables although quoted wrapped inside single quotes , hence not interpolated.

try this:

sed -i "s/^$user/$newuser/" /mypractice/myfiles/testfile 

i have placed ^ anchor in substitution part means substitute if word @ beginning of line. protects making change if name of user not @ start somewhere in middle.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -