Here is a sed
solution by using of d
elete flag:
sed -e '/^-/!d' -e '/^[[:space:]]*$/d' 1.txt > 2.txt
The above command has two regex, the first '/^-/!d'
will match to all lines that doesn't start with -
and they will be deleted from the output, the second '/^[[:space:]]*$/d'
will match to all lines that contains only white spaces and they will be deleted from the output.
sed -e '/^-/d' -e '/^[[:space:]]*$/d' 1.txt > 3.txt
The above command also has two regex, the first '/^-/d'
will match to all lines that starts with -
and they will be deleted from the output, the second is the same as in the previews case.
Another way is to preserve -n
the normal output of sed
and then p
rint only the matched lines:
sed -n '/^-/p' 1.txt > 2.txt
sed -n -r '/^(-|[[:space:]]*$)/!p' 1.txt > 3.txt
Here is a performance test:
$ cat 1.txt | wc -l | sed -r -e 's/([0-9]{6}$)/ \1/' -e 's/([0-9]{3}$)/ \1 lines/'
1 144 270 lines
$ TIMEFORMAT=%R
$ time sed -e '/^-/!d' -e '/^[[:space:]]*$/d' 1.txt > 2.txt
0.357
$ time sed -e '/^-/d' -e '/^[[:space:]]*$/d' 1.txt > 3.txt
0.323
$ time sed -n '/^-/p' 1.txt > 2.txt
0.221
$ time sed -n -r '/^(-|[[:space:]]*$)/!p' 1.txt > 3.txt
0.402
split
is the wrong tool. Usegrep
. Read ` man grep` and theman
pages it refers to.