Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script make lines in one huge file into two seperate files in one go? [duplicate]

Tags:

linux

bash

shell

Currently My shell script iterate the lines in one huge file two times: (What I want to do is just like the shell script below.)

grep 'some_text' huge_file.txt > lines_contains_a.txt
grep -v 'some_text' huge_file.txt > lines_not_contains_a.txt

but it is slow.

How to do the same thing only iterate the lines once?

Thanks!

like image 421
cmal Avatar asked Oct 24 '25 04:10

cmal


2 Answers

With GNU awk:

awk '/some_text/  { print >> "lines_contains_a.txt" }
     !/some_text/ { print >> "lines_not_contains_a.txt" }' huge_file.txt
like image 121
Renaud Pacalet Avatar answered Oct 26 '25 18:10

Renaud Pacalet


With sed:

sed -n '/some_text/  w lines_contains_a.txt
        /some_text/! w lines_not_contains_a.txt'  huge_file.txt
like image 44
M. Nejat Aydin Avatar answered Oct 26 '25 19:10

M. Nejat Aydin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!