test.txt is the dynamic file but some of combination are fix
like below are the lines
;wonder_off =
;wonder_off = disabled
wonder_off =
wonder_off = disabled
the test.txt can content them in any order
#cat test.xt
;wonder_off =
;wonder_off = disabled
wonder_off =
wonder_off = disabled
OR
#cat test.xt
wonder_off =
wonder_off = disabled
;wonder_off =
;wonder_off = disabled
I need to find and replace only 1st instatnce of the test.xt
example if:
#cat test.xt
;wonder_off =
;wonder_off = disabled
wonder_off =
wonder_off = disabled
find ";wonder_off =" and replace it with "wonder_off = "Enabled,working_bar,extra_deft""
Result should be :
cat test.xt
wonder_off = "Enabled,working_bar,extra_deft"
;wonder_off = disabled
wonder_off =
wonder_off = disabled
I can managed to get only 1st line which can be any combination i mention above
#VAR=`cat test.xt | head -1`
#echo $VAR
;wonder_off =
How can I Find only 1st instance with matching string and replace it with "wonder_off = "Enabled,working_bar,extra_deft""
Hi,
Try this untested code
awk 'BEGIN{done=0;}$0 ~ /;wonder_off=/{if(done ==0){gsub(/;wonder_off/,";wonder_off=\"Enabled,working_bar\"",$0);done=1;}print;}' file
pls configure substitute string based on your req.
Cheers,
Ranga:-)
ieth0
September 30, 2011, 2:54pm
3
replace FILE with your file name :
sed -i "`grep -n ^\;wonder_off FILE|cut -d':' -f1 |head -1`s/\;wonder_off =/wonder_off = \"Enabled,working_bar,extra_deft\"/" FILE
for reminder, there are two FILE to replace in the code
Thanks you both
but you missed one VERY IMP thing
the first line can be anything from the combination as i mention
can be
;wonder_off =
can be
;wonder_off = disabled
can be
wonder_off =
can be
wonder_off = disabled
ieth0
September 30, 2011, 6:14pm
5
oh, not a problem , just add a $ at the end of grep regex :
sed -i "`grep -n "^\;wonder_off$" FILE|cut -d':' -f1 |head -1`s/\;wonder_off =/wonder_off = \"Enabled,working_bar,extra_deft\"/" FILE
and its done
now replace
;wonder_off
to anything you want to find , and replace
;wonder_off =
to anything you want to replace.
$ sed 's,;wonder_off =$,wonder_off = "Enabled\,working_bar\,extra_deft",1' infile