Linux的脚本计算的printf参数的数量每次出现 [英] Linux script that counts number of printf parameters for each occurrence

查看:84
本文介绍了Linux的脚本计算的printf参数的数量每次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写的SH脚本,对于每个给定的文件,每个printf的发生,将计%的数量(或printf函数接收参数或者数)和printf的重命名为printfX,当X是数

我卡住了,因为printf的不以新行结束...

例如

 的printf(你好
       天下%d个\\ N,1);

应该返回

  printf1(你好
       天下%d个\\ N,1);


解决方案

有没有简单的(=在脚本)这是因为来解析C code溶液,你需要的 LL(1)语法分析器

但你可以格式化所有的code以同样的方式,使你的脚本可以处理不同的情况。所以,你首先应该做的是找到所有行的printf ,但没有);

 的grep的printf......文件... | grep的-v);

然后修复这些文件,直到你没有得到任何输出了。

I want to write a script in sh that for each given file, for each printf occurrence, it will count the number of %'s (or alternatively the number of parameters the printf function receives) and rename the printf to printfX, when X is that number.

I got stuck because printf doesn't end with new line...

e.g.

printf("hello "
       "world %d\n", 1);

should return

printf1("hello "
       "world %d\n", 1);

解决方案

There is no simple (= as in script) solution for this because to parse C code, you need an LL(1) parser.

But you can format all the code in the same way so your script can handle the different cases. So what you should do first is find all lines with printf but without );:

grep "printf" ...files... | grep -v ");"

Then fix these files until you don't get any output any more.

这篇关于Linux的脚本计算的printf参数的数量每次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆