1

I have a large-ish (250G) MySQL dump file from which I only want / need one database. I was hoping to use awk to split the file up so I did some searching and found this expression:

awk '/START/{x="F"++i;}{print > x;}' file2

Seems like a good start (and works well using their sample file) but running it as:

awk '/CREATE DATABASE/{x="F"++i;}{print > x;}' file2

gives me errors like this:

awk: (FILENAME=dump_all.sql FNR=1) fatal: expression for `>' redirection has null string value

Other pages (from SE) report that this is due to hitting a blank line.

I'd love to use this technique to get the part that I want out of this file but my knowledge of awk syntax is (clearly) quite limited. Is there some way to make this work?

4

I don't know if this will solve your actual issue. Since you haven't shown us the actual input file, I have no idea if what you are attempting will even work. In any case, to fix the error you're getting, use this:

awk '/CREATE DATABASE/{x="F"++i;}(x){print > x;}' file2

That modifies the script to only attempt to print to file x if the variable x is set. The error you were getting was because it was attempting to print all lines, some of which apparently are before the CREATE DATABASE string at which point x was unset. The script above is equivalent to writing:

awk '{
      if(/CREATE DATABASE/){i=i+1; x="F"i;}
      if(x!=NULL){print > x }
     }' file2  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.