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

  • Home
  • SEARCH
  • 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 1059595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:11:51+00:00 2026-05-16T18:11:51+00:00

I want to substitute a certain filed of the text while keeping the same

  • 0

I want to substitute a certain filed of the text while keeping the same blank space:

For example, my text is:

Please              help         me     with this problem
Any                 suggestion   or     help will be appreciated
Thanks              to           all    who  give help

And I want to replace the sentence
“Any suggestion or help will be appreciated”
with
“I want the solution”

So that the text will be:

Please              help         me     with this problem
I                   want         the    solution
Thanks              to           all    who  give help

I have a solution:

awk '{if($1=="Any" && $2=="suggestion" && $3=="or" && $4=="help"  {$1="I";$2="want";$3="the";$4="solution"};print $0}' eg.txt

I will get

Please              help         me     with this problem
I want the solution will be appreciated
Thanks              to           all    who  give help

As you can see, it has two problems

(1) the blank space is not the same with others.

(2) $5, $6,$7 “will be appreciated” of the former line are still kept.

I know another solution:

awk '{if($1=="Any" && $2=="suggestion" && $3=="or" && $4=="help"  print "I                   want         the    solution";print $0}' eg.txt 

will solve the problem.
But I just wondering if there is better way?
Thanks a lot for your attention!

  • 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-16T18:11:52+00:00Added an answer on May 16, 2026 at 6:11 pm

    Here is an example in awk. This code uses Jonathan’s suggestion of printf with a width specifier. match is used to find the correct width.

    #!/usr/bin/awk -f
    BEGIN {
        n1 = split("Any suggestion or help will be appreciated", a1)
        split("I want the solution", a2)
    }
    {
        j = k = 0
        if (NF != n1)
            k  = 1
        for (i = 1; k == 0 && i <= NF; i++)
            if ($i != a1[i])
                k = 1
        if (k) {
            print
            next
        }
        for (i = 1; i <= NF; i++) {
            match(substr($0, j), /[^ ]+ */)
            printf "%-*s", RLENGTH, a2[i]
            j += RLENGTH
        }
        print ""
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Theres is a little problem I want to solve with Haskell: let substitute a
I want to substitute all white space that precedes the class [A-Za-z0-9_] , with
I want to substitute build-in send_mail function, that works only with plain-text emails, with
I'm using Vim, and I want to substitute some placeholder text with a long
I want to store text/string in a text field in a database. This string
want to know why String behaves like value type while using ==. String s1
I want to substitute all 4 instances of the number 300 from the code
I want to use the Full Text Search feature of Microsoft SQL Server. If
I want to substitute a variable in oracle . The var is coming in
I want to substitute, the \ that appears in the Windows directory link to

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.