0

Here is the code in the shell script I have used

sed 's/xyxx/$date1/' /u001/Scripts/abc.html > /u001/Scripts/abc.html 

The code is trying to print HTML tags inside AWK command

nawk 'BEGIN{
      FS=","
      print "<HTML>""<HEAD>""<p>Hi All,<br><br>There are no cases closed on the"
      print  "xyxx that meet the criteria for submission</p>"
      }
      END{
      print "<p>Regards,<br>Support Team</p></BODY></HTML>"
      }
      '
  • You should state your desired result and the actual result. – RalfFriedl Nov 21 '18 at 6:55
  • do u want to replace xyxx with the value of date1 or just as $date1? – msp9011 Nov 21 '18 at 6:56
  • I need to replace xyxx with value of date1 inside the file or can I use print command itself inside awk – RAJESH A S Nov 21 '18 at 7:12
  • use sed -i to edit files in place. Your command will not work, because redirections are done first. So you'll be ending up with an empty file. – pLumo Nov 21 '18 at 11:42
  • Also, if you need variable replacement, you need to put the sed command in double quotes for bash to expand the variables. – pLumo Nov 21 '18 at 11:42
1

In this command,

sed 's/xyxx/$date1/' /u001/Scripts/abc.html > /u001/Scripts/abc.html 

the redirection happens first! So the shell opens /u001/Scripts/abc.html for writing and empties it. Then the sed command runs, but the input file is empty, so no output either.

As the comments suggest, you should use:

sed -i.bak 's/xyxx/'"$date1"'/' /u001/Scripts/abc.html 

which will

  • create a backup file with extension .bak and overwrite the original file (-i.bak)
  • and use the variable value of $date1, because it's put in double quotes, unlike the rest of the sed expression.
  • I am unable to get the output on the mentioned command above sed -i.bak 's/xyxx/'"$date1"'/' /u001/Scripts/abc.html – RAJESH A S Nov 22 '18 at 12:22
  • make sure that $date1 does not contain characters that are interpreted by sed, e.g. a slash or an @ will cause problems. – pLumo Nov 22 '18 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.