codebee
codebee

Reputation: 57

Append multiple files into one (awk)

I am trying to dump all files into one file. For this I was using this awk script (reading.awk)

BEGIN {

 RS = "\n"
 filename= "DDL_dump.sql"
 flag=1;
}

{
    record =$0
    if(record == ") ;")
        flag = 0;
    else if( flag==1)
        print record >> filename 

}

END {
 close(filename)
}

But for every file it is overwriting instead of appending.

Trying to run this from a bat file in same folder as the files.

FOR %%i IN (*.sql) DO awk -f reading.awk %%i

Upvotes: 1

Views: 1180

Answers (1)

hek2mgl
hek2mgl

Reputation: 158230

Looks like you want this (without a shell loop):

awk 'FNR==1{f=1} /\) ;/{f=0} f' *.sql > 'DDL_dump.sql'

Explanation in multiline form:

# True for the first line of each file
FNR==1 {
    f=1 # Set f(lag)
}

# Once the pattern occurs ...
/\) ;/ {
    f=0 # ... reset the (f)lag
}

# As long as the f(lag) is true, print the line.
f

*.sql

is a shell glob expression that expands to all .sql files in the current folder and will pass them to awk as arguments.

> 'DDL_dump.sql'

is shell output redirection that will store the output of the awk command into DDL_dump.sql

Upvotes: 1

Related Questions