Parser


 
Thread Tools Search this Thread
Top Forums Programming Parser
# 1  
Old 01-14-2011
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
# 2  
Old 01-16-2011
in C:
Code:
#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[i] = strlen(key[i]); }

    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[i], val[i]);
                val[i][0] = '\0';
            }

            putchar('\n');
            continue;
        }

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

        if (v == NULL) { continue; }

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

    fclose(fp);
}

or in PERL:
Code:
$/ = "\n\n";
$\ = "\n";
$, = ' ';

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

both are used as follows:
Code:
./parser inputfile

or:
Code:
./parser < inputfile

# 3  
Old 01-17-2011
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:
# 4  
Old 01-17-2011
Code:
$/ = "\n\n";
$\ = "\n";
$, = ' ';

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

# 5  
Old 01-17-2011
ok great looks good. Again thank's for your time.
# 6  
Old 01-17-2011
If you want to csvify the fields:
Code:
/ = "\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)
# 7  
Old 01-18-2011
Code:
#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")))
				;
	}

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Making a parser

input 1..100km 112..403km 500..623km required output 1..51 112..162 500..550 (i.e 50kms added to the initial distance) (2 Replies)
Discussion started by: ANKIT ROY
2 Replies

2. Shell Programming and Scripting

Parser

Hi All, I am trying to create a parser to find out what cobol programs are being called by which JCL's. I need to search recursively until the main cobol program is found being called by a JCL. I tried to create a script but I am not able to generalize it. Can someone please help. ... (1 Reply)
Discussion started by: nua7
1 Replies

3. Shell Programming and Scripting

SQL Parser

Hi, I have been assigned a task to migrate few thousands of sql scripts to a different db format. there could be sub queries and complex joins. there would be functions that needs to be replaced from a given list to another values. this should also parse the sub\inline queries. Can you please... (1 Reply)
Discussion started by: hitmansilentass
1 Replies

4. Shell Programming and Scripting

File Parser

Hi need help parsing a file. I have tag fields and values in a file with delimiter |. sample records from the file listed below 8=value|9=value|35=value|49=value|56=value|34=value|50=value|48=value|10=value 8=value|9=value|35=value|49=value|56=value|34=value|51=value|48=value|10=value... (2 Replies)
Discussion started by: subramanian
2 Replies

5. Shell Programming and Scripting

Parser with sed

Hi, I have this variable: <a href="http://www.rtve.es/mediateca/videos/20100916/video-calamares-rellenos-salsa-pimientos-garbanzos-16-09-10/878586.shtml">V�deo: Calamares rellenos con salsa de pimientos y ...</a> I would like to have: ... (7 Replies)
Discussion started by: mierdatuti
7 Replies

6. Shell Programming and Scripting

xml-parser with perl

Hello I want to write an xml- parser with perl an i use the libary XML::LibXML. I have a problem with the command getElementsByTagName. If there is an empty tag, the getElementsByTagName method returns a NodeList of length zero. how can i check if this is a nodelist of lenght zero?? i... (1 Reply)
Discussion started by: trek
1 Replies

7. Shell Programming and Scripting

need a text parser

i need a simple text parser which can parse a data file created by a softwre so that i can export it to my mysqldb,, datafile created as one record per line with different number of fields. e.g datafile contains following. a=1, b=3, c=4 a=1, c=55, d=abcd a=5, b=hello, c=99, d=help now i... (12 Replies)
Discussion started by: sfaizan
12 Replies

8. Shell Programming and Scripting

Help with an (easy) parser

Hello, i'm workig with a file with structural information about biological macromolecules (proteins etc). In a certain file, the info is structured like this @<TRIPOS>MOLECULE blah 1 blah 2 blah 3 @<TRIPOS>MOLECULE foo 1 foo 2 foo 3 @<TRIPOS>MOLECULE mmm 1 mmm 2 mmm 3 I would... (7 Replies)
Discussion started by: aristegui
7 Replies

9. Shell Programming and Scripting

Text Parser

I am having a text file as follows say server.txt Date Time server ip error code -------------------------------------------------------------------------- 02/21/2008 18:10:14 server1 xxx.xxx.xxx.xxx 6 02/21/2008 08:10:14 server2 ... (8 Replies)
Discussion started by: karthikn7974
8 Replies

10. Shell Programming and Scripting

string parser

I am new to scripting I want to parse a string in a loop eg A:B:C:D E:F:G:H and put them in different variable attr1 = A attr2 = B attr3 = C attr4 = D . . /* do processing with attr1, attr2, attr3 and attr4 */ then go to next line E:F:G:H and again assign... (8 Replies)
Discussion started by: flextronics
8 Replies
Login or Register to Ask a Question