Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:11:12+00:00 2026-06-13T13:11:12+00:00

I am executing a shell script where i should replace string in a file.

  • 0

I am executing a shell script where i should replace string in a file. So of course i am using sed for that. The string is something like:

<!--replace-->
<script src="1.js"></script>
<script src="2.js"></script>
<!--/replace-->

This should be replaced with:

<script src="3.js"></script>

So i am trying to do it as follows:

$ sed "s/<\!--replace-->.\*<\!--\/replace-->/<script src=\"3.js\"><\/script>/g" file.html

But this does not do the trick. What am i missing?

UPD: Removing \n from file is not a solution in this case, simply because file should be readable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:11:12+00:00Added an answer on June 13, 2026 at 1:11 pm

    The problem is related to newlines, you can change your regex to match newlines, or remove newlines
    using tr :

    user@machine:/tmp$ cat s.txt 
    
    <!--replace-->
    <script src="1.js"></script>
    <script src="2.js"></script>
    <!--/replace-->
    
    user@machine:/tmp$ tr "\n" " " < s.txt | sed "s#<\!--replace-->.*<\!--/replace-->#<script src="3.js"></script>#g" 
    
    <script src=3.js></script> 
    

    In case you do not want to remove the newlines, you can :

    a) Try a solution using SED as mentioned here:
    how to tell sed "dot match new line"

    b) use perl or python to replace the multiline regex,with s modifier , the newlines will be intact :

    user@mmmm:/tmp$ cat s.txt 
    
    <!--replace-->
    <script src="1.js"></script>
    <script src="2.js"></script>
    <!--/replace-->
    
    Dummy content containing
    newlines for test.
    
    user@mmmm:/tmp$ perl -i -pe 'BEGIN{undef $/;} s#<\!--replace-->.*<\!--/replace-->#<script src="3.js"></script>#gsm' s.txt 
    
    user@mmmm:/tmp$ cat s.txt 
    
    <script src="3.js"></script>
    
    Dummy content containing
    newlines for test.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need something like make i.e. dependencies + executing shell commands where failing command
I'm executing a shell pipeline from a java program - it'll be something like
while executing a shell script in Unix Bash Shell, say any file in another
In this Bash Shell script I like to check if the length of the
Following the shell script that I am executing #!/bin/sh echo Enter [y/n] : read
I am executing a shell script in which I run a sybase query which
I'm starting a shell script (.cmd) from my .NET application. I'd like the console
I am facing a copy command problem while executing shell script in RHEL 5.
I am trying to get the name of the shell executing a script. Why
I have a KornShell (ksh) script that logins into SQL*Plus and executing a script.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.