Transfer data from log file to excel

Hello,

One thing I think might be helpful for you is a demonstration of the general principle of how to get data out of one file, and into CSV format. So, let's look at a little example.

Imagine we've been asked to produce a CSV file, detailing the stock levels of fruit that we currently have. The inventory is currently described in a text file, like so:

$ cat inventory.txt 
The number of red apples we have is 12
The number of yellow bananas we have is 3
The number of red strawberries we have is 11
The number of blue oranges we have is 0
$ 

Now, this format is consistent, which is important; it's a lot easier to extract data from a file if every record or line in that file is of the same format. In this case, we have all our data one per line, in the same order always, and in the same format. So, that's pretty much perfect for extracting into a CSV.

How do we do that, then ? Well, there are a variety of ways, but here we'll look at one of the most common methods selected in a situation like this, which is to use awk. The solution we're about to see isn't necessarily the most efficient in terms of lines of code or time taken, but it will certainly demonstrate the principle, which is what we're here to do.

At its simplest, then, we can get awk to print a specific numbered field with the print command. The syntax for doing so is as follows:

$ awk '{print $2}' inventory.txt 
number
number
number
number
$ 

Here, we asked awk to print the second field of every line in the file inventory.txt. By default, awk regards fields as being separated by white space. So in this case, the second field of every line is the same - the word "number". It does this for every line in turn, hence our getting four instances of hte word "number" returned.

So, to our solution. Let's say we want to get for each line in this file the name of the fruit, the colour, and the number in stock, in that order. Each field should be separated by commas. We could do that like so:

$ awk '{print $5","$4","$9}' inventory.txt 
apples,red,12
bananas,yellow,3
strawberries,red,11
oranges,blue,0
$

And there we go - by using awk and specifying the fields we wanted and the order in which we wanted them printed (and any other text we wanted to go between them, in this case, commas), we have our desired output.

Hope this helps ! If you have any further questions based on this, or indeed on any of the other responses you've had from others on this thread, please do let us know and we can take things from there.