LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using awk to get blocks of data from a text file (https://www.linuxquestions.org/questions/programming-9/using-awk-to-get-blocks-of-data-from-a-text-file-4175426447/)

ntubski 09-19-2012 09:13 AM

Quote:

Originally Posted by mrpurple (Post 4781832)
I'd definitely be happy to hear of something more efficient.

In my experience, efficiency in shell scripts is usually comes from minimizing the amount of work done in the shell. In this case, I think awk will work a lot better (so we can do zero work in the shell):
Code:

#!/usr/bin/awk -f

BEGIN {
    RS = "\n(\n|$)";
    FS = "\n";
    block = 0;
}

$1 ~ /\[DATATYPE2\]/ {
    for (i = 2; i <= NF-1; i++) {
        printf("processing line %d of block %d\n%s\n",
            i - 2, block, $i);
    }
    block++;
}

Basically, RS = "\n(\n|$)" separates each block, and FS = "\n" puts each line of the block in $1, $2, $3, ... $NF (where $1 is the "[DATATYPE]" line and $NF is the "[END]" line.

grail 09-19-2012 11:10 AM

Well it is a little rough but here is an awk script that does the whole lot. You can change what happens in the END loops to suit your self:
Code:

awk 'BEGIN{ RS = "\\[END[^]]*\\]\n+";FS = "\n"}{for(i = 2; i <= NF; i++)arr[b,c++] = $i;b++;c=0}END{for(x=0; x < b; x++){while( arr[x,y] )print x,y,arr[x,y++];y=0}}' file


All times are GMT -5. The time now is 10:19 AM.