Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep regex return substring but exclude comments

Tags:

regex

grep

I want to get a substring from a file, but only from lines which are not preceded by an exclamation mark (which is the comment symbol in Fortran). I would prefer to use grep (but not bound to). For example:

infile.txt:

calib_ss/baseline.txt
!calib_ss/base_sharpe.txt

Desired result:

baseline

I got this far:

grep -Po "(?<=/)[^/.]*(?=\.)" infile.txt

which returns

baseline
base_sharpe

To exclude the lines starting with ! I thought of combining the expression with

^[^\!]

but I don't manage. Thanks in advance!

like image 459
Daniel_H Avatar asked Oct 04 '22 05:10

Daniel_H


1 Answers

This grep should work:

grep -Po '^[^!].*?(?<=/)\K[^/.]*(?=\.)' infile.txt

OUTPUT:

baseline

Explanation:

  • ^[^!] will make sure to match anything but ! at line start
  • \K will make sure to reset the start
like image 109
anubhava Avatar answered Oct 18 '22 07:10

anubhava