Need help with sed/awk command

Dear ALL,

I am still struggling with some basic sed operations.
I want to change path in a file as shown below:

case_OM = PV4Reader( FileName='/home/linuxUser/demoCases/s1/case/case.OM' )

to

case_OM = PV4Reader( FileName='/home/linuxUser/demoCases/s2/case/case.OM' )

In this file there is only one line starts with "case_OM =".

Can you tell me how to find and replace line starts with specified word/special character?

I want it little more generic as shown below:
as pwd will give path to current directory,
I want to replace complete line

/home/linuxUser/demoCases/s2/case

Thanks & Regards,
linuxUser_

Hi Linux_user
try

nawk '{ if ($0 ~ /^case_OM/) gsub("s1","s2",$0); { print $0 }}' filename

output

[Makarand] # more linux
hello
case_OM = PV4Reader( FileName='/home/linuxUser/demoCases/s1/case/case.OM' )
sdsdsd s1
[Makarand] # nawk '{ if ($0 ~ /^case_OM/) gsub("s1","s2",$0); { print $0 }}' linux
hello
case_OM = PV4Reader( FileName='/home/linuxUser/demoCases/s2/case/case.OM' )
sdsdsd s1

Dear Makarand,

Thanks a lot for the reply. But this is case specific. I want to replace complete path with pwd of the running path.
Any idea?

Regards,
linuxUser_

Buddy
try

[Makarand] # nawk -v var=`pwd` '{ if ($0 ~ /^case_OM/) gsub($2,var,$0); { print $0 }}' FS="'" linux
hello
case_OM = PV4Reader( FileName='/hww/appl/test/testv/bin/mak_test' )
sdsdsd s1
[Makarand] # pwd
/hww/appl/test/testv/bin/mak_test

if you want to replace with full path of file and not pwd then use following

nawk -v var=`find $PWD -type f | grep "case.OM"` '{ if ($0 ~ /^case_OM/) gsub($2,var,$0); { print $0 }}' FS="'" linux
1 Like

Thanks again :slight_smile:

With sed:

sed '/^case_OM/ s#s'\''[^'\'']*'\''#'$PWD'#' file

NB '\'' is a literal ' within a 'string' :
' ends the string, followed by \' outside the string, followed by ' that starts the remaining part of the string.