本文主要是介绍Linux在C中调用shell命令获取文件的行数和列数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//实现在linux下C中调用shell语句获取某个文本中内容的行数和列数
//cat ./data/test.txt | grep -n " " | awk -F ":" '{print $1}' |tail -n1
//cat ./correction/test.txt | awk -F ' ' '{print NF}' | head -n1
#define MAX_BUFF_LEN (128)
void getRowAndCol(char * formatPath)
{//cat ./data/test.txt | grep -n " " | awk -F ":" '{print $1}' |tail -n1 //cat ./correction/test.txt | awk -F ' ' '{print NF}' | head -n1 int row, col; char cmd[MAX_BUFF_LEN] = {0}; char buffer[MAX_BUFF_LEN]= {0}; FILE *pf; sprintf(cmd, "cat %s | grep -n \" \" | awk -F \":\" '{print $1}' | tail -n1",formatPath); system(cmd); pf = popen(cmd, "r"); fread(buffer, sizeof(buffer), 1, pf); row = atoi(buffer); pclose(pf);memset(cmd, 0, MAX_BUFF_LEN); memset(buffer, 0, MAX_BUFF_LEN); sprintf(cmd, "cat %s |awk -F ' ' '{print NF}' | head -n1", formatPath);system(cmd); pf = popen(cmd, "r"); fread(buffer, sizeof(buffer), 1, pf); col = atoi(buffer); pclose(pf);
}
这篇关于Linux在C中调用shell命令获取文件的行数和列数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!