Head and hail at the same time

Is there one liner liner for this?

head -n 3 test.txt > out_dir/test.head.txt
grep hello test.txt > out_dir/test.tmp.txt
cat out_dir/test.head.txt out_dir/test.tmp.txt > out_dir/test.hello.txt
rm out_dir/test.head.txt out_dir/test.tmp.txt

Ie, I want to get the header and some grep lines from the given file at the same time.

+4
source share
5 answers

Use awk:

awk 'NR<=3 || /hello/' test.txt > out_dir/test.hello.txt
+7
source

You can say:

{ head -n 3 test.txt ; grep hello test.txt ; } > out_dir/test.hello.txt
+8
source

sed

sed -n '1,3p; /hello/p' test.txt > out_dir/test.hello.txt
+3

awk , sed :

$ sed -n test.txt -e '1,3p' -e '4,$s/hello/hello/p' test.txt > $output_file

-n , , . -e - '1,3p prints ou 4,$s/hello/hello/p , hello, hello . p .

There should be a way to use it 4,$g/HELLO/p, but I could not get it to work. It has been a long time since I really messed up sed.

+3
source

Of course I would go awk, but here is the edpre- nostalgic solution for pre- vi:

ed test.txt <<%
4,$ v/hello/d
w test.hello.txt
%
+2
source

All Articles