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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:00:41+00:00 2026-05-26T19:00:41+00:00

Assuming using bash , having a configuration file like: param-a=aaaaaa param-b=bbbbbb param-foo=first occurence <–

  • 0

Assuming using bash, having a configuration file like:

  1. param-a=aaaaaa
  2. param-b=bbbbbb
  3. param-foo=first occurence <– Replace
  4. param-c=cccccc
  5. # param-foo=first commented foo <– Commented: don’t replace
  6. param-d=dddddd
  7. param-e=eeeeee
  8. param-foo=second occurence <– Rreplace
  9. param-foo=third occurence <– Last active: don’t replace
  10. param-x=xxxxxx1
  11. param-f=ffffff
  12. # param-foo=second commented foo <– Commented: don’t replace
  13. param-x=xxxxxx2

In which you can find multiple commented or uncommented lines of the param-foo,
how can you comment all the uncommented param-foos except the very last active one,
resulting in:

  1. param-a=aaaaaa
  2. param-b=bbbbbb
  3. # param-foo=first occurence <– Replaced
  4. param-c=cccccc
  5. # param-foo=commented foo <– Left
  6. param-d=dddddd
  7. param-e=eeeeee
  8. # param-foo=second occurence <– Replaced
  9. param-foo=third occurence <– Left
  10. param-x=xxxxxx1
  11. param-f=ffffff
  12. # param-foo=second commented foo <– Left
  13. param-x=xxxxxx2

Two parts of the question:
1. How to do it with only one known repeating param?
(only param-foo in the example above)

2. How to do it with all multiple active params at once?

(param-foo + param-x in the example above)
Attention: In this case I don’t know previously the name of the repeating params!

Thanks

  • 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-26T19:00:41+00:00Added an answer on May 26, 2026 at 7:00 pm

    If awk is acceptable, this will do it for param-foo and param-x:

    awk -F= -v p='param-foo param-x'  'BEGIN { 
      ARGV[ARGC++] = ARGV[ARGC - 1]
      n = split(p, t, OFS)
      for (i = 0; ++i <= n;) _p[t[i]]
      }
    NR == FNR {
        $1 in _p && nr[$1] = NR
      next
      }  
    $1 in nr && FNR != nr[$1] {
      $0 = "# " $0 
      }1' infile
    

    You may use a single parameter: p=param-x or add more parameters separated by spaces: p=’param-1 param-2 … param-n’.

    Edit: I’m assuming the real input file looks like this:

    param-a=aaaaaa
    param-b=bbbbbb
    param-foo=first occurence
    param-c=cccccc
    # param-foo=commented foo
    param-d=dddddd
    param-e=eeeeee
    param-foo=second occurence
    param-foo=third occurence
    param-x=xxxxxx1
    param-f=ffffff
    param-x=xxxxxx2
    

    Let me know if it’s different.

    Second edit: providing a solution for mawk users:

    awk -F= -v p='param-foo param-x'  'BEGIN { 
      n = split(p, t, OFS)
      for (i = 0; ++i <= n;) _p[t[i]]
      }
    NR == FNR {
        $1 in _p && nr[$1] = NR
      next
      }  
    $1 in nr && FNR != nr[$1] {
      $0 = "# " $0 
      }1' infile infile
    

    Adding solution for the latest requirement:

    awk -F= 'NR == FNR {
      if (NF && !/^#/)
        _p[$1]++ && nr[$1] = NR
      next
      }    
    $1 in nr && FNR != nr[$1] {
      FNR != nr[$1] && $0 = "# " $0 
      }1' infile infile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming the file exists (using os.path.exists(filename) to first make sure that it does), how
I am using curl from a BASH shell. I would like to create a
Today's Daily Vim says this: Assuming you're using the bash shell, the following can
Assuming I'm using some graphic API which allows me to draw bezier curves by
What is the differences between those 2 assuming I'm using SQL Server as my
Assuming I change the contents of a control using a XamlReader and add the
Using (local) in the connection string doesn't work on my cluster. I'm assuming it's
How do I retrieve the temperature of my CPU using Python? (Assuming I'm on
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
i am using MySQL with PHP & Doctrine 2. my question is assuming i

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.