读取配置文件[方式二]之使用Awk实现

读取配置文件[方式二]之使用Awk实现

分类: C++ Linux系统编程 C 2010-03-11 09:52 368人阅读 评论(0) 收藏 举报

一.配置文件样本:

[c-sharp] view plain copy
  1. # user.conf  
  2. # username age sex country  
  3. tom 20 male us  
  4. chen 22 female cn  

二.使用shell的awk语言进行解析读取

例如:

获取 tom 的性别则可以使用这样的语句:

[c-sharp] view plain copy
  1. cat user.conf | awk '/^tom[[:blank:]]+/ {print $3}'  

三.在C语言中,我们如何将使用awk得到的结果交给一个变量呢?

一般就我而言采用两种方式,一种则是将得到的结果写入一个temp文件,然后直接读取到某个buffer中;另一种则是通过管道的方式写进去,再读出来,如

http://blog.csdn.net/wqf363/archive/2007/01/28/1496231.aspx讲到的例子一样。

[c-sharp] view plain copy
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <unistd.h>  
  4.   
  5. static int my_system(const char* pCmd, char* pResult, int size);  
  6. int main(void)  
  7. ...{  
  8.     char result[1025];  
  9.     my_system("cat user.conf | awk '/^tom[[:blank:]]+/ {print $3}'", result, 1025);  
  10.     printf("the result is: %s ", result);  
  11.     return 0;  
  12. }   
  13.   
  14. static int my_system(const char* pCmd, char* pResult, int size)  
  15. ...{  
  16.     int fd[2];  
  17.     int pid;  
  18.     int count;  
  19.     int left;  
  20.     char* p = 0;  
  21.     int maxlen = size-1;  
  22.     memset(pResult, 0, size);  
  23.     if(pipe(fd))  
  24.     ...{  
  25.         return -1;  
  26.     }  
  27.     if((pid = fork()) == 0)  
  28.     ...{// chile process  
  29.         int fd2[2];  
  30.         if(pipe(fd2))  
  31.         ...{  
  32.             return -1;  
  33.         }  
  34.         close(1);  
  35.         dup2(fd2[1],1);  
  36.         close(fd[0]);  
  37.         close(fd2[1]);  
  38.         system(pCmd);  
  39.         read(fd2[0], pResult, maxlen);  
  40.         pResult[strlen(pResult)-1] = 0;  
  41.         write(fd[1], pResult, strlen(pResult));  
  42.         close(fd2[0]);  
  43.         exit(0);  
  44.     }  
  45.     // parent process  
  46.     close(fd[1]);  
  47.     p = pResult;  
  48.     left = maxlen;  
  49.     while((count = read(fd[0], p, left))) ...{  
  50.         p += count;  
  51.         left -= count;  
  52.         if(left == 0)  
  53.         break;  
  54.     }  
  55.     close(fd[0]);  
  56.     return 0;  
  57. }  

 

执行结果:

[c-sharp] view plain copy
  1. [denny@localhost testforc]$ ./a.out   
  2. the result is: male  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在shell中运行命令时,你经常需要从配置文件读取参数和选项。以下是一些常用的shell脚本读取配置文件的方法: 1. 使用source命令读取配置文件使用source命令可以将配置文件中的变量和函数导入到当前shell中。例如,如果你的配置文件名为config.sh,可以使用以下命令读取: ``` source config.sh ``` 这样就可以在当前shell使用config.sh中定义的变量和函数。 2. 使用点号(.)命令读取配置文件: 点号(.)命令与source命令的作用相同,都可以将配置文件中的变量和函数导入到当前shell中。例如,如果你的配置文件名为config.sh,可以使用以下命令读取: ``` . config.sh ``` 这样就可以在当前shell使用config.sh中定义的变量和函数。 3. 使用cat和while命令读取配置文件: 你可以使用cat命令读取配置文件的内容,并使用while命令逐行处理配置文件中的每个条目。例如,如果你的配置文件名为config.txt,其中每行都包含一个变量名和一个值,你可以使用以下命令读取: ``` cat config.txt | while read line; do eval "$line" done ``` 这将逐行读取config.txt文件,并使用eval命令将每个行中的内容转换为shell命令并执行。 4. 使用awk命令读取配置文件: 你可以使用awk命令读取配置文件中的每个变量和值,并将它们转换为shell变量。例如,如果你的配置文件名为config.txt,其中每行都包含一个变量名和一个值,你可以使用以下命令读取: ``` awk -F= '{print "export " $1"="$2}' config.txt | while read line; do eval "$line" done ``` 这将读取config.txt文件,并使用awk命令将每个变量和值转换为export命令,然后使用eval命令将export命令执行,将变量导入到当前shell中。 以上是一些常用的读取配置文件的方法,你可以根据自己的需要选择合适的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值