0

I have gone through other question which are similar to mine. Following command works, but it doesn't work if I'm trying to remove all the lines except the lines which has string ".c" or ".h".

sed -r -n -e '/.java|.c/p' test.txt


/home/jenkins/workspace/Test/base/src/packages/.c
/home/jenkins/workspace/Test/base/src/packages/.txt
/home/jenkins/workspace/Test/base/packages/Manager.java
Mihir
  • 557
  • 1
  • 6
  • 28
  • It sounds like `grep -v '\.[ch]'` would do what you want; your command doesn't seem to try to do anything with `h`. Also, what's the underlying problem you're trying to solve? You seem to handle file paths, so maybe shell globbing would be more appropriate. – Benjamin W. Sep 05 '18 at 18:31
  • @BenjaminW. - yes, my command doesn't have .h but all i wanted was grep or sed command which will remove all the lines except the matching pattern.. – Mihir Sep 05 '18 at 18:36

1 Answers1

2

With GNU sed:

sed -n '/\.c\|\.h/p' file

or

sed -n -E '/\.c|\.h/p' file

or

sed -E '/\.c|\.h/!d' file

See: The Stack Overflow Regular Expressions FAQ

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • You don't have to group if your group contains the whole regex anyway, do you? `/\.c|\.h/` should work. (Or `/\.[ch]/`, at that.) – Benjamin W. Sep 05 '18 at 20:24