0

I tried to find and replace certain strings in text file and the replace text contains "/" and sed commond did not work. then I tried with awk command and it works fine with terminal with fixed variables. but when I tried to use it with parameters in the loop it dose not work. I have attached the code that I have write so far. could you please find me a solution. this works find in terminal awk '{sub(/input_image/,"https://www.abcd.com/images/XPDDL_R1_20161007.jpg")}1' format2.svg > format2.html

x=1
for param in ${paramO[@]}
do
    awk '{sub(/${param}/,"${paramN[x]}")}1' $output_file > temp.txt
    cp -rp temp.txt $output_file
    let x=x+1
done

  • can you provide a sample input and expected output for better understanding. – Gautham Sreenivasan Nov 04 '22 at 09:24
  • This is horribly inefficient anyway. Read the strings into Awk or `sed` and perform all the replacements in a single script. – tripleee Nov 04 '22 at 09:34
  • Tangentially, try https://shellcheck.net/ before asking for human assistance; your script contains several common beginner mistakes which are distracting if not directly responsible for some of the problems you are asking about. – tripleee Nov 04 '22 at 09:35
  • The immediate problem is that single quotes do not allow the shell variable to be visible to Awk. – tripleee Nov 04 '22 at 09:35
  • @GauthamSreenivasan solved it with sed -i "s|${param}|${paramN[x]}|g" $output_file thank you for the support! – Nadeesh Subasinghe Arachchige Nov 07 '22 at 02:25

0 Answers0