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 8594031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:06:58+00:00 2026-06-12T00:06:58+00:00

I am new to shell scripting and am attempting to solve a problem. Basically,

  • 0

I am new to shell scripting and am attempting to solve a problem.

Basically, I need to find instances of a REGEX pattern in a text file, do some sed editing on each result, and then output the result in place to the original text file without disturbing any other original text.

The mental block I’m facing is that a) I need to do multiple pattern replacements in order to accomplish the final transformed result, so it can’t be fit into a single call to sed, and b) if I grep out the results with a REGEX pattern, it gives me only the results that match that pattern. Ok great, so I go ahead and edit those to transform them into the final desired output, but I want to write this change exactly in place where it was in the text file. You can’t pipe grep to sed -i because it doesn’t reference the original file anymore, just the result set. Am I thinking about this in the wrong way or is there some command I’m just not aware of?

  • 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-12T00:06:59+00:00Added an answer on June 12, 2026 at 12:06 am

    You can “grep” before applying commands with sed’s facilities for addressing:

    sed '/grep-like regex/ s/pattern to replace/replacement/' -i file
    

    Where the “-i file” tells sed to edit “file” in place.

    Multple commands can be given ans separated either through semicolons or through the command line with multiple “-e” options:

    sed -e '/regex1/ s/pattern1/replacement/' -e '/regex2/ s/pattern2/replacement/'
    sed '/regex1/ s/pattern1/replacement/; /regex2/ s/pattern2/replacement/'
    

    If in the example above regex1 and regex2 are the same, you can also group the replacement commands into a single block, by using backets:

    sed '/regex/ { s/pattern1/replacement/; s/pattern2/replacement/ }'
    

    or for the sake of clarity:

    sed '/regex/ {
            s/pattern1/replacement/
            s/pattern2/replacement/
        }'
    

    You can also further filter the lines to apply the commands by nesting the /regexes/:

    sed '/regex1/ {
            s/pattern1/replacement/
            /regex2/ s/pattern2/replacement/
        }'
    

    So the second substitute command is executed only if regex1 and regex2 match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to shell scripting and I need some help on the following
I am new with shell scripting. I am having a file containing some records
I'm pretty new to shell scripting and linux in general. Basically, I need to
I am new to UNIX Shell scripting. I need help in removing leading and
I am very new to the shell scripting world. I have done some research
I am new to Unix shell scripting and would like some help with writing
I'm very new in shell scripting, and I encountered a problem that is quite
I am pretty new to shell scripting and I have to add a flag
I am new to shell scripting. so kindly bear with me if my doubt
I am new at shell scripting and I am trying to read data from

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.