Cut ad delete

Hi i need to cut some rows matching an ip, delete it and redirect all on an other file. I did this:

cat all_devices.log | awk '/192.168.0.254/' > routerA.log
sed -i '/192.168.0.254/d' all_devices.log

Now i want to rewrite all in a single sed command. But i can't find the best way. Any suggestion?

Thanks

Please post sample data and required output.

Ok!
I have this log:
Aug 31 08:54:23 Parallels[92]: Loading Virtual Ethernet module...
Aug 31 08:54:32 Parallels[104]: Initialization complete.
Aug 31 08:54:32 192.168.0.254 yyyyyyyyyy xxxxxx
Aug 31 08:54:52 192.168.0.254 : yyyyyyyyyyyy zzzzzzzzzz
Aug 31 08:54:31 Parallels[97]: Staring DHCP/NAT daemon...

And i want to extract the 192.168.0.254 rows and copy that in an other logfile.
The "clean" log should be like this:
Aug 31 08:54:23 Parallels[92]: Loading Virtual Ethernet module...
Aug 31 08:54:32 Parallels[104]: Initialization complete.
Aug 31 08:54:31 Parallels[97]: Staring DHCP/NAT daemon...

and this is the new log:
Aug 31 08:54:32 192.168.0.254 yyyyyyyyyy xxxxxx
Aug 31 08:54:52 192.168.0.254 : yyyyyyyyyyyy zzzzzzzzzz

But i'm trying to do this in ony one command row.

Hi.

awk '/192.168.0.254/ {print > "file1"; next} { print > "file2"}' infile

Sed -i (in-place) can lead to unexpected results. I'll suggest to use a temporary file.

awk '{split($4,d,".");file=(d[4]?$4:"new")".log";print >> file;close(file)}' old.log && mv new.log old.log

This is just a basic example :wink:

This will work for any ip

awk ' ($4 ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ ) {print > "file1" ;next } {print > "file2"}' infile