我正在使用C代码和sed。我想阅读间隔1-10,11-20等行来执行一些计算。
int i,j,m,n;
for(i=0;i<10;i++){
j=i+1;
//correction. m,n is modified which was incorrect earlier.
m=i*10;
n=j*10;
system("sed -n 'm,n p' oldfile > newfile");
}外卖。
m,n p它看起来变量没有在系统中传递。有什么办法吗?
发布于 2020-12-10 13:27:52
使用sprintf构建命令行:
char cmdline[100];
sprintf(cmdline, "sed -n '%d,%dp' oldfile.txt > newfile.txt", 10*i+1, 10*(i+1));
puts(cmdline); // optionally, verify manually it's going to do the right thing
system(cmdline);(这很容易发生缓冲区溢出,但如果命令行参数不太灵活,则100字节就足够了。)
发布于 2020-12-10 13:28:05
在C中不能替换字符串文字的一部分,您需要的是
patterns
sprintf()/snprintf()将是你的朋友。您可以做类似的事情(复制pmg的评论)
char cmd[100];
snprintf(cmd, 100, "sed -n '%d,%dp' oldfile > newfile", 10*i+1, 10*(i+1));
system(cmd);https://stackoverflow.com/questions/65235342
复制相似问题