Extracting multiple lines OR deleting multiple lines from a file using AWK

Extracting multiple lines OR deleting multiple lines from a file using AWK

Post by shwe » Fri, 13 Sep 2002 01:47:24



Hi there,

I have a file with few sql statements in it and am trying to:

1. Extract all the lines other than lines starting from "copy"
including the following "\p\g" using AWK
       OR
2. Delete the lines starting from "copy " including the following
"\p\g"

Problem is that the copy statement is repeting quite a few times.

Here's the sample file:                 Here's the output I want"

create table demo(                      create table demo(
        demo1 char(10),                 demo1 char(10),
        demo2 char(20)                  demo2 char(20)
)                                       )
with duplicates,                        with duplicates,
location = (ii_database),               location = (ii_database),
security_audit=(table,norow)            security_audit=(table,norow)
\p\g                                    \p\g
copy demo (                             set journaling on demo
        demo1= varchar(0)tab,           \p\g
        demo2= varchar(0)nl,            create table hello(
        nl= d1)                                 hello1 char(10),
from 'C:/demo.ing'                                      hello2 char(20)
with row_estimate = 2                           )
\p\g                                    with duplicates,
set journaling on demo                  location = (ii_database),
\p\g                                    security_audit=(table,norow)
create table hello(                     \p\g
        hello1 char(10),
        hello2 char(20)
)
with duplicates,
location = (ii_database),
security_audit=(table,norow)
\p\g
copy hello (
        hello1= varchar(0)tab,
        hello2= varchar(0)nl,
        nl= d1)
from 'C:/hello.ing'
with row_estimate = 2
\p\g

Here's the script I wrote which is almost working by taking copy
statement out until the following "\p\g". But I want the following
"\p\g" out as well.

BEGIN {
        state=0;
      }

/^copy.*/ {
             if (state==0) {
                state = 1
             }
          }

/^\\p\\g/ {
             if (state==1) {
                state=0
             }
          }

          {
            if (state==0) {
             print
            }
          }

Any ideas would be appreciated.

Thanks in advance.

 
 
 

Extracting multiple lines OR deleting multiple lines from a file using AWK

Post by Floyd Davidso » Fri, 13 Sep 2002 02:32:26



>Hi there,

   [snip]

Quote:>Here's the script I wrote which is almost working by taking copy
>statement out until the following "\p\g". But I want the following
>"\p\g" out as well.

>BEGIN {
>        state=0;
>      }

>/^copy.*/ {
>             if (state==0) {
>                state = 1
>             }
>          }

>/^\\p\\g/ {
>             if (state==1) {
>                state=0
>             }
>          }

What happens if the following block is placed between
the above two blocks, so that the print can only happen
_prior_ to changing state back to 0.

Quote:>          {
>            if (state==0) {
>             print
>            }
>          }

>Any ideas would be appreciated.

>Thanks in advance.

BEGIN {state=0;}

/^copy.*/ {if (state==0) {state = 1} }
          {if (state==0) {print}     }
/^\\p\\g/ {if (state==1) {state = 0} }

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>


 
 
 

Extracting multiple lines OR deleting multiple lines from a file using AWK

Post by John W. Krah » Fri, 13 Sep 2002 06:11:20



> I have a file with few sql statements in it and am trying to:

> 1. Extract all the lines other than lines starting from "copy"
> including the following "\p\g" using AWK
>        OR
> 2. Delete the lines starting from "copy " including the following
> "\p\g"

> Problem is that the copy statement is repeting quite a few times.

> Here's the sample file:                 Here's the output I want"

> create table demo(                      create table demo(
>         demo1 char(10),                 demo1 char(10),
>         demo2 char(20)                  demo2 char(20)
> )                                       )
> with duplicates,                        with duplicates,
> location = (ii_database),               location = (ii_database),
> security_audit=(table,norow)            security_audit=(table,norow)
> \p\g                                    \p\g
> copy demo (                             set journaling on demo
>         demo1= varchar(0)tab,           \p\g
>         demo2= varchar(0)nl,            create table hello(
>         nl= d1)                                 hello1 char(10),
> from 'C:/demo.ing'                                      hello2 char(20)
> with row_estimate = 2                           )
> \p\g                                    with duplicates,
> set journaling on demo                  location = (ii_database),
> \p\g                                    security_audit=(table,norow)
> create table hello(                     \p\g
>         hello1 char(10),
>         hello2 char(20)
> )
> with duplicates,
> location = (ii_database),
> security_audit=(table,norow)
> \p\g
> copy hello (
>         hello1= varchar(0)tab,
>         hello2= varchar(0)nl,
>         nl= d1)
> from 'C:/hello.ing'
> with row_estimate = 2
> \p\g

This will work and should be fairly easy to translate to AWK.  :-)

perl -ne'BEGIN{$/="\\p\\g\n"}!/^copy/&&print'

John
--
use Perl;
program
fulfillment

 
 
 

Extracting multiple lines OR deleting multiple lines from a file using AWK

Post by Sony E Anton » Fri, 13 Sep 2002 09:53:16


Quote:>>security_audit=(table,norow)
>>\p\g
>>copy hello (
>>        hello1= varchar(0)tab,
>>        hello2= varchar(0)nl,
>>        nl= d1)
>>from 'C:/hello.ing'
>>with row_estimate = 2
>>\p\g

> This will work and should be fairly easy to translate to AWK.  :-)

> perl -ne'BEGIN{$/="\\p\\g\n"}!/^copy/&&print'

I m not very good in perl. But this only deleted the first copy - \p\g
block. The succeeding ones appeared.

sed '/^copy/,/\\p\\g/d' <file>

should work for multiple copy - \p\g blocks.

Couldn t figure out if this can be directly translated to awk.

--sony

 
 
 

1. How to extract multiple lines using awk

Hi All,

I have a file that has following lines:

apple 123
456

orange 789
101112

pear 131415
161718

I would like to extract following lines from this file using awk: awk 'BEGIN { FS="\n"; RS="\n\n"} /apple/ {print $0}'

apple 123
456

But it only outputs first line:

apple 123

Any help is appreciated!

2. .cshrc equivalent for LINUX?

3. Using awk or the shell to parse a file containing multiple line groups of data

4. Compose key

5. Find a string, delete that line, delete 1 line before it and all lines after it...

6. why does telnetd refuse connection?

7. find and replace multiple lines using awk or sed

8. Performance Comparison Celeron vs Pentium II

9. Fold multiple lines using sed or awk

10. Grep/awk/sed for multiple/groups of lines in a file???