I want to process a log file(using C) in which the data are separated by double colons “::”. I suppose tools like awk and sed will be quite efficient to do this. But is it possible that awk output values can pass to my C variable?
For example, one log info is like this:
a::b::c::d
and I have a struct consists of 4 int elements. How to save the 4 parts into the struct elements?
I’ve thought of using strtok libary call as another way, but before calling it, I have to sort out the suitable lines from a whole bunch of log information, and that makes me think more about the sed or grep.
Thank you for your help. If you have better choices, that will be very kind of you to share it.
update: I forgot to emphasize that everything mentioned above is done in runtime including “sort out the matched lines according to a given value”, and process them, then store the value. scanf is fine, but what if there is string value with spaces inside?
You can always use a mixture of tools. For example, let’s say your log file is in the file
log.out.Then you can use
grepto select the lines you are interested in:You could either store it as another file, or directly give it to your program:
Assuming your program is named
analyze_data. In the source of the program, you simply read fromstdinand store the values you want:Of course with proper error checking.