How to Split File to 2 depending on condition?

Hi ,

cat myfile.txt

! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.22.11 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.2.50 ! 3100.2.22.11 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.20.11 ! 
! 3100.2.0.5 ! 3100.2.11.1 ! 3100.2.60.07 ! 3100.2.4.1 !

i need to split this file to 2 file , the first contains all the lines with occurrence the 3100.2.22.4 and absecne of 3100.2.22.11

and the second file contains all other lines , the result should be like below ,

myfile1

! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.20.11 !

myfile2

! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.22.11 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.2.50 ! 3100.2.22.11 ! 3100.2.4.1 !
! 3100.2.0.5 ! 3100.2.11.1 ! 3100.2.60.07 ! 3100.2.4.1 !

can you help me with this please

Just thinking...
what if you number all lines (cat -n)
then do you grep command
pull out all those line numbers into file_exclude
cut to remove the file numbers
use a grep with the contents of file_exclude

(Sorry could not go into more; got to go. But thought this might spur thought)

really i have alot of those files every seconds , i'm looking for maybe awk command to solve this quickly without genrating alot of temp files ,

How about applying the things you learned earlier?

Hi Rudic , really i tried to do it like before but i dont know how to use match to get not matching values as one Condition ( when match 3100.2.22.4 and not match 3100.2.30.33 in the same line ) , i will be thankful again if you changed the condition to fit this

man awk might help. However. try

awk     '/3100.2.22.4/ && !/3100.2.22.11/       {print > "myfile1"; next}    
                                                {print > "myfile2"}    
        ' file
1 Like

Thanks "MAN" again