Delete all lines except a line starting with string

Shell : bash
OS : RHEL 6.8

I have a file like below.

$ cat pattern.txt
hello
txt1
txt2
txt3
some other text
txt4

I want to remove all lines in this file except the ones starting with txt . How can I do this ?

gnu?

sed -i '/^txt/!d' pattern.txt

Hi,

You could try;

cat pattern.txt | sed '/^txt/!d'

Regards

Gull04

grep '^txt' pattern.txt
grep -v '^txt' yourfile >file_without_txt