But i have many occurances of "SOURCEFILE" in test.cfg , i need to search only for this line and replace that line with
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
again there are many occurences of sun4v in that file but i need to replace only in this line. I tried grep for "SOURCEFILE="and using perl -pi -e but it replaces wherever it finds "sun4v" but i want only on this line.
I tried your option but again it does replaces all the occurences of sun4v but i want to look for that line and replace the occurence only in that line.
but for some reasons it didnt work too. Actually iam executing this script as a finish script when the machine is jumpstarted.
Once the machine is build i checked the file it had no changes, but if i execute it manually it does change.I couldnt figure out whats the problem.
Please help me.
sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/4v/4x/' filename
P.S. Note that you may need to insert the whole pattern after the s command ...
ex:
sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' filename
Iam sorry but that one failed too. It replaced everywhere it found sun4v.
Is there anyway i could insert a comment ('#') in front of that line and append a new line with the new variable next to that line.
Iam sorry i didnt see ur edited message.
Here's the command i tried
sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' test.cfg
It returns nothing , the file remains unchanged.
Also i tried the nawk script the file remains unchanged and it prints the whole file with no changes in it.
Hm,
may be there is an error in the pattern,
did you copy/paste-it in your post, or you modified it by hand?
Here is what I get:
$ uname -r
5.8
$ cat file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
Similar to the above lines there are lots of variable declarations with the variable SUN4V
#SOURCEFILE_LOCATION---------------------------
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
$ nawk '{
if(!(checked)&&($0~/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/)){
print "#"$0
sub(/4v/,"4x")
checked=1
print
}else{
print
}
}' file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
Similar to the above lines there are lots of variable declarations with the variable SUN4V
#SOURCEFILE_LOCATION---------------------------
#SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
With sed:
$ sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
Similar to the above lines there are lots of variable declarations with the variable SUN4V
#SOURCEFILE_LOCATION---------------------------
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
$ cat file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
Similar to the above lines there are lots of variable declarations with the variable SUN4V
#SOURCEFILE_LOCATION---------------------------
SOURCEFILE=/usr/platform/sun4v*/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v*/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
$ nawk '{
if(!(checked)&&($0~/SOURCEFILE=\/usr\/platform\/sun4v[*]\/driver\/file.cfg/)){
print "#"$0
sub(/4v[*]/,"4x")
checked=1
print
}else{
print
}
}' file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
Similar to the above lines there are lots of variable declarations with the variable SUN4V
#SOURCEFILE_LOCATION---------------------------
#SOURCEFILE=/usr/platform/sun4v*/driver/file.cfg
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v*/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE