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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:24:26+00:00 2026-05-13T22:24:26+00:00

I have moved my classes from a global namespace into a specific namespace. I

  • 0

I have moved my classes from a global namespace into a specific namespace. I have also changed the class names. I want to convert all my source files that use these classes to the new format. I was thinking either a bash script using a sed file on Cygwin or executing a perl script. I’m having troubles with the bash script.

Here’s the process I’m trying to perform:

  1. For each source file, *.cpp and
    *.hpp, recursively:
  2. If the file contains an old class
    name, then convert the file.
  3. End for.

My problem in Bash script is detecting if the file contains an old class name. I was thinking of one grep statement for each class name:

for f in `find . -iname \*.[ch]pp`;
do
  if [ grep "Field_Blob_Medium" $f -eq 0 || grep "Field_Boolean" ]; then
      sed -f conversion.sed $f
  fi
done

An issue is that only one command can be in Bash if statement, using this syntax:

if grep "Field_Unsigned_Short" $f;

so I can’t do a logical OR of greps.

I could perform a nested loop, but I don’t know how to break out of a Bash for loop:

OLD_CLASS_NAMES="Field_Blob_Medium Field_Boolean Field_Unsigned_Int"
for f in `find . -iname \*.[ch]pp`;
do
  for c_name in $OLD_CLASS_NAMES;
  do
     if grep $c_name $f then
        sed -f convert.sed $f # <-- Need to break out of inner loop after this execution.
     fi
  done
done

So I’m looking for suggestions on how to process every source file that contains old class names and convert them to new ones. A Cygwin Bash script example is preferred, although a Perl script would also be acceptable. Also, the script should make a backup copy of the original source file before writing the converted contents out to the new file.

I’m running Cygwin on Windows XP and Windows Vista.

  • 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-13T22:24:27+00:00Added an answer on May 13, 2026 at 10:24 pm

    Don’t shy away from Perl: it makes this sort of task easy!

    #! /usr/bin/perl -i.bak
    
    use warnings;
    use strict;
    
    my $old = join "|" => qw(
      Field_Blob_Medium
      Field_Boolean
      Field_Unsigned_Int
    );
    
    chomp(@ARGV = `find . -iname \*.[ch]pp -print0 |
                   xargs -0 grep -lE '($old)'`);
    
    unless (@ARGV) {
      warn "$0: nothing to do!\n";
      exit 0;
    }
    
    while (<>) {
      s[Field_Blob_Medium]  [my::ns::MediumBlob]g ||
      s[Field_Boolean]      [my::ns::Bool]g       ||
      s[Field_Unsigned_Int] [my::ns::UInt]g;
    
      print;
    }
    

    The -i switch is for in-place editing. It automatically creates backups and writes the transformed results to the original locations.

    Setting @ARGV makes it as though you had invoked the program with only those *.cpp and *.hpp files that contain the old class names. The -E switch to grep enables extended regular expressions so unescaped (, ), and | are metacharacters.

    If there were no hits (i.e., if @ARGV is empty), then there’s nothing to do.

    Otherwise, for each line of each file that has the old names (the mechanics of which Perl handles for you!), try to rename Field_Blob_Medium to my::ns::MediumBlob and so on. Note that these substitution attempts cease after the first success, so if a line contains two different old names, one will be replaced and the other will remain the same.

    Usually the substitution operator is written s///, but you may use bracketing delimiters as above. I did so to left-justify the new names.

    Of course this is a stand-in for your actual substitution.

    Finally, print the possibly-modified line, which because of -i writes it to the updated source file.

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

Sidebar

Related Questions

I have recently moved from Eclipse 3.4 to 3.5. In 3.4 I had a
I understand that a C++ library should use a namespace to avoid name collisions,
From research on Stack Overflow and other sites I'm 99% sure that the problem
I have a Silverlight application communicating with the server side through WCF services. Initially
For a little background: I have a DLL project with the following structure: Rivworks.Model
I'm writing a game and an accompanying engine in C++. The engine relies heavily
When I learned PHP it was pretty much in procedural form, more recently I've
I've spent a great long while googling this problem without any luck and I've
I've recently been reading Louis Davidson's book on Sql Server Database Design and found

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.