Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    37

    Unanswered: Shell script to SEARCH multiple string occurances

    I need to search the strings in a text file that has the following information:


    INSERT INTO TEST123
    values (1,2,3);

    INSERT INTO ABCD
    values (1,2,3);

    UPDATE TEST123
    SET
    ABC='123'
    WHERE ABC='124';

    UPDATE ABCD
    SET
    ABC='123'
    WHERE ABC='124';

    DELETE FROM TEST123
    WHERE ABC = '111'
    AND DEF= '222'
    AND GHI='333';


    DELETE FROM ABCD
    WHERE ABC = '111'
    AND DEF= '222'
    AND GHI='333';

    How could I Search for the string "TEST123" and then search for the following character ";".

    Once these two strings are found, I want to Copy the data from line1(FIRST STRING FOUND) and lineN(SECOND STRING FOUND) to another file..


    NEW FILE OUTPUT:


    INSERT INTO TEST123
    values (1,2,3);

    UPDATE TEST123
    SET
    ABC='123'
    WHERE ABC='124';

    DELETE FROM TEST123
    WHERE ABC = '111'
    AND DEF= '222'
    AND GHI='333';


    Any help is appreciated

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Try this:
    Code:
    #!/bin/ksh
    awk '{
    if ( index($0,"TEST123") > 0 ) flg=1;
    if ( flg == 1 ) {print $0;
    if ( index($0,";") > 0 ) {print ""; flg=0;}}
    }' InFile.txt >OutFile.txt
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    or a bit terser:

    nawk -v str='TEST123' -f gomes.awk InFile.txt

    Code:
    BEGIN {
      RS=FS=""
    }
    
    ($1 ~ str) { print $0, "\n" }
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  4. #4
    Join Date
    Feb 2004
    Posts
    37
    It is throwing the following error

    + nawk -v str=TEST123 -f gomes.awk InFile.txt
    nawk: input record `UPDATE TEST123..' too long

  5. #5
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Quote Originally Posted by gomes009
    It is throwing the following error

    + nawk -v str=TEST123 -f gomes.awk InFile.txt
    nawk: input record `UPDATE TEST123..' too long
    if on Solaris, try /usr/xpg4/bin/awk instead of nawk.
    or if you have gawk installed - use it instead.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

Posting Permissions

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