Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys, I have been working on this since 3 days but wasn't able to achieve what I want I have a big text that has the following format: Current ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9

    [SOLVED] Help in script - Getting table name from a text file

    Hi guys,

    I have been working on this since 3 days but wasn't able to achieve what I want

    I have a big text that has the following format:

    Current max fieldLen for table1 (a):
    Fld# Width MaxWidth ERR NAME
    ---- ------ -------------- ------ ---------
    2: 80 38 *** field-name
    3: 4 2 field-version
    4: 40 7 field-value
    5: 90 116 value-meaning

    Current max fieldLen for table2 (b):
    Fld# Width MaxWidth ERR NAME
    ---- ------ -------------- ------ ---------
    2: 80 38 field-name
    3: 4 2 field-version
    4: 1998 720 definition
    5: 2 2 data-type
    8: 40 12 field-format
    10: 4 2 data
    11: 80 19 split-length

    Current max fieldLen for table3 (c):
    Fld# Width MaxWidth ERR NAME
    ---- ------ -------------- ------ ---------
    2: 10 5 uic
    3: 2 1 user-type
    4: 4 2 chess-rels
    5: 4 0 next-rels
    6: 6 0 next-rels-active
    7: 16 3 *** security-format
    8: 30 6 default-applid
    9: 15 6 next-txid
    10: 80 19 user-desc
    12: 16 5 ci-acc-group
    13: 40 0 ci-password

    Current max fieldLen for table4 (d):
    Fld# Width MaxWidth ERR NAME
    ---- ------ -------------- ------ ---------
    3: 40 9 type
    2: 120 193 *** description

    Current max fieldLen for table5 (e):
    Fld# Width MaxWidth ERR NAME
    ---- ------ -------------- ------ ---------
    2: 10 5 aic
    3: 16 5 name
    4: 30 8 austpac-address
    5: 10 0 user
    26: 20 5 route-profile

    As you see, the column headers are fix. Also, I have the following fixed words in the beginning of every table "Current max fieldLen for".
    For some of these lines, in the error column, there are 3 stars.

    Note: The table name is the word that could be found after "Current max fieldLen for".

    Basically, I am searching for a way in order to get the table name in case the table has "***" in the error column, in any of its fields.

    For example, for this sample I provided, I need the output to be exactly:

    table1
    table3
    table4


    Why not table2 or table5? Because they don't have any "***"

    For now, I did a little bit of progress by using:
    "
    egrep "(^Current|\*\*\*)" filename.txt > test.txt
    cat test.txt | awk '{print $5}' > test2.txt
    "
    Could anyone help please?

    Very much appreciated!

    Regards.

    NOTE: Sorry for the bad format of the text file. I am attaching an image with the proper format
    Last edited by wiss; 03-14-2011 at 08:49 AM.

  2. #2
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    1] That image is not of a great help
    2] Print table names:
    #!/usr/bin/awk -f
    /Current max fieldLen/ {
    print $5
    }
    3] The tricky part is extending the above into a script that prints only table names with ***, so let's try to output only ***:
    #!/usr/bin/awk -f
    /:/ {
    if ($4=="***")
    print $4
    }
    4] And finally combine it into the final script:

    #!/usr/bin/awk -f
    /Current max fieldLen/ {
    table=$5
    }
    /:/ {
    if ($4=="***")
    print table " has " $4
    }
    Testing it produces the following output:

    $ ./test.awk test.txt
    table1 has ***
    table3 has ***
    table4 has ***
    I'm a awk novice and all i had to read was written here: AWK Programming

    --- EDIT
    Code:
    #!/usr/bin/awk -f
    /Current max fieldLen/ {
            table++
            result[table,0]=$5
            result[table,1]="false"
            result[table,2]=0
    }
    /^[0-9]*:/ {
            result[table,2]++;
            if ($4=="***") {
                    result[table,1]="true"
            }
    }
    END {
            print "table    |       ***     |       fields"
            print "---------------------------------------"
            for(i=1; i<=table; i++)
                    print result[i,0] "     |       " result[i,1] " |       " result[i,2]
    }
    Last edited by Kloschüssel; 03-11-2011 at 09:14 AM.

  3. #3
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9
    Thanks a lot for your help!!

    I will try it soon and let you know the outcome. Sorry about the photo, i didn't realize it was so small!!!
    Last edited by wiss; 03-11-2011 at 10:49 AM. Reason: fixing errors

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Using your data on file "data1" with perl script "p1":
    Code:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate reading in "paragraph" mode.
    
    use strict;
    use warnings;
    
    my ($table);
    
    $/ = "";    # Read "paragraphs" as a single line
    
    # Table name is string after "Current max fieldLen for"
    
    while (<>) {
      m{Current max fieldLen for\s+(\w+)};
      if ( defined($1) ) {
        $table = $1;
        print "$table\n" if m{[*]{3}};
      }
    }
    
    exit(0);
    produces:
    Code:
    % ./p1 data1 
    table1
    table3
    table4
    I used the property of your data that "***" does not appear elsewhere in a table.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9
    Hi Kloschüssel,

    I am not sure why this script is not going through the whole table. It's just displaying the first 2 tables that have "***"

  6. #6
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9
    Hi DRL,


    Thanks a lot for your help! It looks working perfectly

    In case I have tables that have a dash included in the name, this script just picks the first part. example:
    user-id will be displayed as user

    Any idea how to take the whole name? (ie. user-id)

    Thanks in advance

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Adding to your dataset with a name such as you described, and a changed perl script line:
    Code:
    m{Current max fieldLen for\s+(\S+)};
    I get:
    Code:
    % ./p2 data1 
    table1
    table3
    table4
    name-with-dashes
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    I am not sure why this script is not going through the whole table. It's just displaying the first 2 tables that have "***"
    Can't tell without an example. Probably those lines do not match the regex '/Current max fieldLen/' or '/^[0-9]*:/'?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...