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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:17:13+00:00 2026-06-16T19:17:13+00:00

This is similar to Using sed to replace beginning of line when match found

  • 0

This is similar to Using sed to replace beginning of line when match found but a different question so hence this thread.

I wish to uncommented out commented out code. More specially, all cases where the variable myVar is commented out.

Example:

public class MyClass {
   ...
   ...
   //myVar.setAge(200);
   //myVar.setPlanet("mars");
}

to

public class MyClass {
   ...
   ...
   myVar.setAge(200);
   myVar.setPlanet("mars");
}

The regex:

^\\.*myVar.*$

Gets me everything I need.

The tricky part is getting the correct Sed. I try:

sed 's/(^\\)(.*myVar.*$)/\2/g' Build_DM_Digests_Batch.cls

On the following basis. Create two match groups. The first one is the commented out lines. And the second is the rest of line. Replace the entire line with just the second mateched group.

This gives error:

sed: -e expression #1, char 29: Unmatched ) or \)

Any tips?

  • 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-16T19:17:15+00:00Added an answer on June 16, 2026 at 7:17 pm

    Use sed 's/^\( *\)\/\/\(.*myVar.*$\)/\1\2/' file

    $ cat hw.java 
    class hw {
        public static void main(String[] args) {
            System.out.println("Hello World!"); 
    //        myVar=1
            //myVar.setAge(200);
            //myVar.setPlanet("mars");
        }
    }
    
    $ sed 's/^\( *\)\/\/\(.*myVar.*$\)/\1\2/' hw.java
    class hw {
        public static void main(String[] args) {
            System.out.println("Hello World!"); 
            myVar=1
            myVar.setAge(200);
            myVar.setPlanet("mars");
        }
    }
    

    Use the -i option to save the changes in the file sed -i 's/^\( *\)\/\/\(.*myVar.*$\)/\1/' file:

    Explanation:

    ^      # Matches the start of the line
    \(     # Start first capture group  
     *     # Matches zero or more spaces
    \)     # End first capture group
    \/\/   # Matches two forward slashes (escaped)
    \(     # Start second capture group 
    .*     # Matches anything 
    myVar  # Matches the literal word 
    .*     # Matches anything
    $      # Matches the end of the line
    \)     # End second capture group 
    

    Here we capture the whitespace upto the // and then everything after if myVar on the line and replace with \1\2.

    Your logic is almost there but a couple of things, firstly escaped all brackets and secondly you want ^( *)\/\/ not ^\\ that is two escaped forwardslashes with the whitespace captured not two backslashes at the start of the line:

    If you don’t want to escape brackets you need to use the extended regexp flag of sed which is -r for GNU sed on OSX it’s -E so check with sed --help.

    sed -r 's/^( *)\/\/(.*myVar.*$)/\1\2/' file
    

    Note: when you are matching the whole line (from ^ to $) the g flag is redundant.

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

Sidebar

Related Questions

This is similar to the following question: Ignore comments (#) using sed, but keep
I'm using the followin RegEx which I found in this similar question : /[^\s"]+|"[^"]*"/g
Similar to this question I am using a custom VirtualPathProvider to retrieve views that
Currently I am using a post-build event command line similar to this one: xcopy
I have checked this similar question, but the suggestions did not solve my problem:
I saw this similar question here but can't figure out how to use Contains
this is a question similar to this one . But I am not good
Note: This similar SO question talks about how to build the same class, but
I have already looked at this similar question but i am still wondering if
This is similar to: How to open a file using JavaScript? Goal: to retrieve/open

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.