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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:22:01+00:00 2026-05-25T06:22:01+00:00

I need to replace two patterns in each line of a file. Lets say

  • 0

I need to replace two patterns in each line of a file. Lets say for example the file contains the following:

pattern1 a1 b1 c1
pattern2 a2 b2 c2
pattern1 a1 b1 c1

Basically, anywhere “pattern1” appears, a1 needs to replaced with a2 and b1 for b2, and so on. However, I also want to change pattern1 into pattern2.

So far I have something that comes kind of close:

/pattern1/ s/a1 b1 c1/a2 b2 c2/

Note, I want to keep the entire sed script in a single file.

UPDATE
Desired output, this may not be the best example, I’ll answer any questions.

pattern2 a2 b2 c2
pattern2 a2 b2 c2
pattern2 a2 b2 c2
  • 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-05-25T06:22:01+00:00Added an answer on May 25, 2026 at 6:22 am

    There are various ways to do it.

    sed -e '/pattern1/{
            s/a2/a1/
            s/b2/b1/
            s/c2/c1/
            s/pattern1/pattern2/
            }' "$@"
    

    You can often collapse that to a single line by using semi-colons in place of newlines (but some versions of sed are fussier about that than others). The -e isn’t strictly necessary in this context.

    It isn’t entirely clear from the question whether ‘a1’ should be replaced by ‘a2’ or vice versa; this code works off the wording “a1 needs to be substituted for a2” (meaning “replace occurrences of a2 with a1”), but the example suggests the opposite substitution. If the other meaning is required, then use lines like s/a1/a2/ instead of s/a2/a1/.

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

Sidebar

Related Questions

I have a file which contains several text entries, say one two three Now,
I need to find two adjacent repeating digits in a string and replace with
I need to replace character (say) x with character (say) P in a string,
working on an ANT build file, where I have two macros defined and need
How do I replace two double quotes into single one using jQuery? For example:
How do I replace a string that contains --- to - ? For example
In this function I need to replace all chars in a file that is
I need to replace the values of the two replica (A and B) in
I need replace slide effect to fade-out/fade-in effect on the prev and next slide
Need to replace a domain name on all the links on the page that

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.