English 中文(简体)
通过档案和进行操纵的壳牌照
原标题:Shell script to loop through files and do string manipulation

我想在目录的多个档案中进行操纵。

我基本上有一份项目目录,有m文档,我想研究所有档案;发现所有已实现国家统一分类(关键,评论)和改换为国家标准化统计系统(关键,表,评论)。

简单的Find & 替换无法工作,因为我需要在我之间插入表格,而且我无法人工操作,因为通过该项目至少有12万例。

逻辑像我这样认为:

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

  • 发现所有“本土化规范”的情况

  • 复制变数中的“钥匙”和“comment”;

  • 将NS LocalizedString(key, comment)改为NS LocalizedStringFromable(var1, table, var2)

  • (除此以外)

因此,我如何写这本文字?

最佳回答

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.

其次,使用<代码>的,以在这些结果内替换案文。 (与澳门有关的<条码>的<>编码/代码”版本不同于“国联<条码>,将不适当处理新的条码和其他特殊性质”。) 如果你不为你工作的话,你也许需要 gr。

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."

查询模式,/NS LocalizedString(((*)、(*)/,发现案文,然后复制(......) tags(必须绕过<>条码>的括号(<<>>>>>>)。

替换模式,/NS LocalizedStringFromTable (1,表2)/,改为NS localizedStringFromTable,然后把第一部和第二版<代码>(*)的正数改为<1和2

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"

如果你打算用不同的价值代替你在原岗位上提到的“瓦尔1”和“瓦尔2”,那么你就需要具体说明。

问题回答

既然这是一个可可项目,那么你为什么只能利用整个项目在民主选举学会中的发现和取代(欧洲复兴开发银行)? 这是它所需要的。

你们需要张贴几个样本,以了解替代模式,但这似乎只是一个简单的问题。 了解你可以使用<代码>[>*?,以在上停止使用

An example would be

 (NSLocalizedString()("["^]*?)(,)("["^]*?)()) ,  123#TABLE#345 

这一例子包括:NS LocalizedString("var1”、“var2”),作为投入,将其编成NS LocalizedString ("var1”,#table#,“var2”





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签