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

The Archive Base Latest Questions

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

I want to do string manipulation in multiple files in a directory hierarchy. I

  • 0

I want to do string manipulation in multiple files in a directory hierarchy.

I basically have a project directory with .m files and I want to look into all the files; find all instances of NSLocalizedString(key, comment) and change that to NSLocalizedStringFromTable (key, table, comment).

Simple Find & Replace cannot work because I need to insert the ‘table’ in between and I cannot do this manually as there are at least 1200 instances of this through the project.

The logic would be something like this I guess:

  • loop through the directory structure to find all .m files

  • find all instances of “NSLocalizedString”

  • copy the “key” and “comment” in variables var1 & var 2

  • replace NSLocalizedString(key, comment) with NSLocalizedStringFromTable(var1, table, var2)

  • save the file (not replace it, save it)

So how do I write the script to do this?

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

    If you open Terminal, cd into the directory that contains these files, I find this to be the best to find those files:

    find . -type f -name "*.m*" -print


    This finds everything within the current . directory that is a file -type f that has a name of *.m, *.mm, *.mmmmm, and so on (the *.m*). You then output each resulting file -print to the console (or a pipe). If you wish to pass each of the files to another process (using xargs), it is best to replace -print with -print0 so it correctly handles whitespace in the filenames.

    Next, use sed to replace the text within those results. (The version of sed that comes with Mac is different from the GNU sed, and will not properly handle newlines and other special characters correctly. You may need to grab that version if this does not work for you.)

    The basic structure of the replacement is:

    sed -i "" -e 's/NSLocalizedString(\(.*\), \(.*\))/NSLocalizedStringFromTable(\1, table, \2)/g' "FILENAME"


    The -i "" replaces the file in-place (saves it to the same file that was opened). -e just means the next text is going to be an expression. Starting the expression with s/ signifies that you are going to be doing a search-and-replace. It is usually in the format:

    s/[search for this pattern]/[replace with this pattern]/g


    The /g at the end means ‘global’, or “do this for as many instances that are found on each line as possible.”

    The search pattern, /NSLocalizedString(\(.*\), \(.*\))/, finds that text and then copies the contents of the \(...\) tags (you have to escape the parentheses so sed knows to remember it).

    The replacement pattern, /NSLocalizedStringFromTable(\1, table, \2)/, replaced the NSLocalizedString with NSLocalizedStringFromTable, and then tucked the exact replacement of the first and second \(.*\) pairs into the \1 and \2 references.

    If you had this literal value:

    NSLocalizedString(@"Darn tootin'", @"How they say 'that is correct' in some dialects");


    then the result would become:

    NSLocalizedStringFromTable(@"Darn tootin'", table, @"How they say 'that is correct' in some dialects");

    Now, @shellter asked in the comments whether you meant that you want the literal word table to be in there, whether parameter 1 or parameter 2 should come from different tables, etc. That would certainly change the format of this search string.

    Lastly, you can combine the two above features into one long shell script and run it in Terminal:

    find . -type f -name "*.m*" -print0| \
    xargs -0 -I FILENAME \
    sed -i "" -e 's/NSLocalizedString(\(.*\), \(.*\))/NSLocalizedStringFromTable(\1, table, \2)/g' "FILENAME"
    

    If you meant for different values to be in place of “var1” and “var2” that you mentioned in your original post, you’ll need to specify.

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

Sidebar

Related Questions

I am using LaTeX and I have a problem concerning string manipulation. I want
I have a HTML string and want to replace all links to just a
I want to build an SQL string to do database manipulation (updates, deletes, inserts,
i am trying to do a simple string manipulation. input is murder, i want
I have a XHTML string I want to replace tags in for example <span
I have multiple configuration files describing Addins to my system. I'm looking for a
Using VB.NET - I have a string: ##RES00012##Some value ##RES00034##Another value That I want
I have a DateTime string in 24 HOURS format .. 2009-12-31T23:59:59 I want it
I have a string like: string originalStringBefore = http://www.abc.com?a=||ThisIsForRndNumber||&qq=hello&jj=||ThisIsForRndNumberAlso||; I want every string which
I'm trying some string manipulation in my web app, but I don't want 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.