Create a new file from another file with lines begins with a specific string

Hi,

I have a file with below format
myfile.txt

aa
aa1
aa2
qq
aa4
ghs
aa6
bbc
gdf

I m looking to create a file where lines begins with aa.

myfile1.txt

aa
aa1
aa2
aa4
aa6

Regards,
Litu

What have yuo tried so far?

cat /myfile.txt | awk '$1 ~ /^ aa*/' > /mynew.txt

---------- Post updated at 08:17 PM ---------- Previous update was at 08:13 PM ----------

awk '/aa/' /myfile.txt > newfile.txt

this seems to work. However i am looking for something without use of awk command.

Hello Litu,

awk can read the file so there is no need to add cat command there. You have got UUCA award :slight_smile: Useless Use of Cat Award

Also you can remove starting space in following /^aa/ then you will get the correct output.

EDIT: Changed aa* to aa in suggestion. grep command can help us here for same.

grep "^aa" filename

Output will be as follows.

aa
aa1
aa2
aa4
aa6

Thanks,
R. Singh

Try:

grep '^aa' file > newfile

----

That should be

/^aa/
1 Like