Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep lines that end with .c or .cpp?

Tags:

linux

shell

I have a file as below, I want to grep for lines having .c or .cpp extension. I have tried using cat file|grep ".c" grep but I am getting all types of extensions as output. Please shed some light on this. Thanks in advance.

file contents are below:
/dir/a/b/cds/main.c
/dir/a/f/cmdss/file.cpp
/dir/a/b/cds/main.h
/dir/a/f/cmdss/file.hpp
/dir/a/b/cdys/main_abc.c
/dir/a/f/cmfs/file_123.cpp
like image 628
sree Avatar asked Sep 18 '25 13:09

sree


1 Answers

grep supports regular expressions.

$ grep -E '\.(c|cpp)$' input
  • -E means 'Interpret PATTERN as an extended regular expression'
  • \. means a dot .
  • () is a group
  • c|cpp is an alternative
  • $ is the lineend

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!