Split a file which a word criteria in two files with awk

Hello,

I'm searching with the Awk command to split a file into two others files.
I explain :

in the file N�1 I search the word "NameVirtual" and since that word to the end of the file I want to store all lines in a new file N�2

Also from that word to the beginning of the file I want to store all lines in a file N�3

I have make a command with awk but I find just the line with the word criteria
I'm a beginner with awk

Could you help me for that

Thanks a lot

Christian

One way:

awk '/NameVirtual/{f=1}{print > f?"file2":"file3"}' file1

hello,

i have tried that awk syntax but it doesn't work.
I have also tried other command with grep but no results.

So an example
Word criteria is : NameVirtualHost

file1:

aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
NameVirtualHost
dddddddddddd
eeeeeeeeeee
ffffffffffffffffff

with awk i must put in file2
--------------------------
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc

and in file3
-----------
NameVirtualHost
dddddddddddd
eeeeeeeeeee
ffffffffffffffffff

With grep i can select all line with the Word Criteria but it's not the request

So do you have another idea

thanks a lot

bye

What doesn't work? What errors did you get?

Try nawk or /usr/xpg4/bin/awk on Solaris.

Hello,

thanks for your answers, it's very nice.

The errors are syntax errors but i'm beginner on awk:

i put the command as so:
awk '/NameVirtualHost/ {f=1} {print > f? "toto.txt":"titi.txt"}' httpd.conf

awk: syntax error near line 1
awk: illegal statement near line 1

I have read the awk man but i didn't found whay it is

Thanks, bye

Have you tried it with nawk or /usr/xpg4/bin/awk if you have Solaris?

HEllo,

i'm trying with nawk because i work on Solaris 10, i will inform you

have a lot of thanks, bye

---------- Post updated at 09:30 AM ---------- Previous update was at 09:26 AM ----------

hello,

here is the result, i not good with nawk

nawk '/NameVirtualHost/ {f=1} {print > f? "toto.txt":"titi.txt"}' httpd.conf
nawk: syntax error at source line 1
context is
/NameVirtualHost/ {f=1} {print > >>> f? <<<
nawk: illegal statement at source line 1

thanks,

The command works fine for me. OK try this:

nawk '/NameVirtual/{f=1} f{print > "file2";next} {print > "file3"}' file1

Hi.

Just for info: On Solaris 10, for me the nawk doesn't work (which is quite odd!).

/usr/xpg4/bin/awk does work though.

Hello,

it's ok it's like seems to work, just like adaptations and it runs.

Many thanks, i will reply when it works right as I want it

Thanks bye

---------- Post updated at 10:57 AM ---------- Previous update was at 10:02 AM ----------

Hello,

the following command works now fine, but just to find the first Word criteria
NameVirtualHost it find the word #NameVirtualHost and I do not want it just NameVirtualHost

I have tried but it doesn't work

nawk '/^#NameVirtualHost/{f=1} f{print > "httpd.conf.production";next} {print > "httpd.conf.reference"}' httpd.conf

Have you an idea to disable the #NameVirtualHost

Many Thanks bye

Try:

/^NameVirtualHost$/{f=1}

or:

{if($0 == "NameVirtualHost"){f=1}}

Hello,

ok it works fine

Many, many Thanks