Bài đăng

Đang hiển thị bài đăng từ Tháng 2, 2019

JSON-C Example

JSON-C Example So you want to parse JSON with C? Welcome aboard! First get json-c, configure, compile, and update your ld cache wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz tar xf json-c-0.9.tar.gz cd json-c-0.9 ./configure && make && sudo make install sudo ldconfig Now use one of the test files provided or this example: http://www.jroller.com/RickHigh/entry/json_parsing_with_c_simple cd .. vim json-example.c #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include "json.h" int main(int argc, char **argv) { struct json_object *new_obj; MC_SET_DEBUG(1); // I added some new lines... not in real program new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"pageCount\": 100, \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID...

Displaying IP address on eth0 interface

Displaying IP address on eth0 interface Awk ifconfig eth0 | awk '/inet addr/{split($2,a,":"); print a[2]}' split function in the above awk command splits the second column based on the delimiter  :  and stores the splitted value into an associative array  a . So  a[2]  holds the value of the second part. sed ifconfig eth0 | sed - n '/inet addr/s/.*inet addr: *\([^[:space:]]\+\).*/\1/p' In basic sed ,  \(...\)  called capturing group which is used to capture the characters. We could refer those captured characters through back-referencing.  \([^[:space:]]\+\)  captures any character but not space one or more times. grep ifconfig eth0 | grep - oP 'inet addr:\K\S+' \K  discards the previously matched characters from printing at the final and  \S+  matches one or more non-space characters. Perl ifconfig eth0 | perl - lane 'print $1 if /inet addr:(\S+)/' One or more non-space characters which are next to th...