Parser

Hi Everyone

I have an out put of multiple lines
which I would like to parse and retrieve certain info from it.

The output consists of multiple sections that starts with the line Client:
and ends with STL tag: each section separated by an empty line.

So basically somehting like
Client: mem-yd-n02b
Backup ID: mem-yd-n02b_1293862169
Policy: mem-yd-win-ms
Policy Type: MS-Windows-NT (13)
Proxy Client: (none specified)
Creator: root
Name1: (none specified)
Sched Label: full_weekly
Schedule Type: FULL (0)
Retention Level: 1 month (3)
Backup Time: Sat Jan 1 01:09:29 2011 (1293862169)
Elapsed Time: 2034 second(s)
Expiration Time: Tue Feb 1 01:09:29 2011 (1296540569)
Compressed: no
Client Encrypted: no
STL tag: *NULL*

I would want to put this through a parser to reteive only what I want and put it on one line

ex:
Client: mem-yd-n02b Backup ID: mem-yd-n02b_1293862169 Policy: mem-yd-win-ms

Thanks

in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char *key[] = {
    "Client:",
    "Backup ID:",
    "Policy:",
    "Elapsed Time:",
};
const int N = sizeof(key) / sizeof(char *);

void main(int argc, char **argv) {
    char *              name = *argv++;
    char *              file = *argv++;
    FILE *              fp   = stdin;

    size_t              len[N];
    char                val[N][BUFSIZ];

    int                 i;

    char                buf[BUFSIZ];

    for (i = 0; i < N; i++) { len = strlen(key); }

    if (1 < argc) {
        fp = fopen(file, "r");

        if (fp == NULL) {
            perror(file);
            exit(1);
        }
    }

    while (fgets(buf, sizeof(buf), fp)) {
        char *p = NULL;
        char *v = NULL;

        if (*buf == '\n') {
            for (i = 0; i < N; i++) {
                if (0 < i) { putchar(' '); }
                printf("%s %s", key, val);
                val[0] = '\0';
            }

            putchar('\n');
            continue;
        }

        for (i = 0; i < N; i++) {
            if (! strncmp(buf, key, len)) {
                p = buf + len + 1;
                v = val;
            }
        }

        if (v == NULL) { continue; }

        while (*p != '\0') {
            if (*p == '\n') { *p = '\0'; }
            *v++ = *p++;
        }
    }

    fclose(fp);
}

or in PERL:

$/ = "\n\n";
$\ = "\n";
$, = ' ';

while (<>) {
    my %X = m{^([^:]+):\s+([^\n]*)$}gm;
    print map { $_ . ':', $X{$_} } 'Client', 'Backup ID', 'Policy';
}

both are used as follows:

./parser inputfile

or:

./parser < inputfile

Hi
First off thank-you for the time and effort you put into this.
As for the Perl script if I would want to pull other info from the file. How would I add
the code necessary to retrieve lets say Retention Level:

$/ = "\n\n";
$\ = "\n";
$, = ' ';

while (<>) {
    my %X = m{^([^:]+):\s+([^\n]*)$}gm;
    print map { $_ . ':', $X{$_} } 'Client', 'Backup ID', 'Policy', 'Retention Level';
}

ok great looks good. Again thank's for your time.

If you want to csvify the fields:

/ = "\n\n";
$\ = "\n";
$, = ',';

my @H = ( 
  'Client',
  'Backup ID',
  'Policy',
  'Retention Level'
);

sub csv { print map { '"' . $_ . '"' } @_; }

csv @H;

while (<>) {
    my %X = m{^([^:]+):\s+([^\n]*)$}gm;
    csv map { $X{$_} } @H;
}

(I was bored)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[])
{

	char info[BUFSIZ];
	char temp[BUFSIZ];

	while(fgets(temp,sizeof(temp),stdin)){
			if(temp[0] == '\n') continue;
			sscanf(temp,"%s %s",info,info);
			printf("Client:%s ",info);
			scanf("%s %s %s",info,info,info);
			printf("Backup ID:%s ",info);
			scanf("%s %s",info,info);
			printf("Policy:%s\n",info);
			getchar();

			while(fgets(info,sizeof(info),stdin) && strncmp("STL",info,strlen("STL")))
				;
	}

}

Hi homeboy
How would I use your script?
:wall:

egrep -e '^Client:|^Backup ID:|^Policy:' yourfile | tr ' \n' '_ ' | xargs -n3

when your inputfile is a.txt and you wanna your output to be b.txt,then
compile that code and name the executable file "program",
program < a.txt > b.txt

$ echo "Client: mem-yd-n02b
Backup ID: mem-yd-n02b_1293862169
Policy: mem-yd-win-ms
Policy Type: MS-Windows-NT (13)
Proxy Client: (none specified)
Creator: root
Name1: (none specified)
Sched Label: full_weekly
Schedule Type: FULL (0)
Retention Level: 1 month (3)
Backup Time: Sat Jan 1 01:09:29 2011 (1293862169)
Elapsed Time: 2034 second(s)
Expiration Time: Tue Feb 1 01:09:29 2011 (1296540569)
Compressed: no
Client Encrypted: no
STL tag: *NULL*" | egrep -e '^Client:|^Backup ID:|^Policy:' | sed 's/: /:/g;s/ /_/g' | tr '\n' ' ' | xargs -n3
Client:mem-yd-n02b Backup_ID:mem-yd-n02b_1293862169 Policy:mem-yd-win-ms
$