NAWK to remove lines that matches a specific pattern

Hi,

I have requirement that I need to split my input file into two files based on a search pattern "abc"

For eg. my input file has below content

abc
defgh
zyx

I need file 1 with
abc

and file2 with
defgh
zyx

I can use grep command to acheive this. But with grep I need to scan the input file twice to get the desired output. Also with huge files grep just hangs the system.

Can you please help me if I can use NAWK for this requirement?

Thanks

A more suitable input file as an input will make more sense.

awk '/^abc/{print > "file1"; next}1' inputfile > file2

Hi Frank,

Thanks. It worked. Could you please tell me if I can pass the pattern, name of file1 and file2 as parameters to AWK.

Thanks

If file1 is in variable file1_name and file2 is in variable file2_name

awk '/^abc/{print > file1; next}1' file1=$file1_name inputfile > $file2_name

Thanks Anurag,

Can I pass pattern also as a parameter. In my case "abc" is pattern that has to be searched

if pattern value(abc) is in pat variable then:

awk '$0 ~ p {print > file1; next}1' p="^$pat" file1=$file1_name inputfile > $file2_name

Reading a file twice may not be too bad since grep is very efficient. On the other hand, processing it with "print > file" in awk will not be ideal since awk have to find which file to write and flush the file for each record (even though it only read the file once). I am sure there are other more efficient ways.