Find and replace

Hi Team,

one silly issue. but its not working for me.

I need to find a pattern a file and replace it with the given value in another file.

Here's the code snippet.

Search_String=100
Replace_String=151
cat  ${work}/temp_${CSV_File}  |   sed  's|"${Search_String}"|"${Replace_String}"|g'  >  $work/${work_1}/${CSV_File}

That means all the occurences of 100 will have to be replaced by 151. The value of Replace_String will be incrementing 1 in loop. If i hardcode the value, the function works. If I pass it through variable Search_String and Replace_String, the find and replace it not happening. Can anyone help me to fix the issue?

Regards,
KM

---------- Post updated at 03:48 AM ---------- Previous update was at 03:21 AM ----------

I got it.

If we use " (double quotes) instead of ' (single quotes), it will work.

cat  ${work}/temp_${CSV_File}  |   sed  "s|${Search_String}|${Replace_String}|g"  >  $work/${work_1}/${CSV_File}

You dont need cat here. Also, use double quotes:

Search_String=100
Replace_String=151
sed "s|${Search_String}|${Replace_String}|g" ${work}/temp_${CSV_File} > $work/${work_1}/${CSV_File}
1 Like

Thank you bartus11.