Saturday, February 5, 2022

[SOLVED] How to extract head lines starting with a specific char (leading spaces neglected)?

Issue

I'm trying to extract the comment lines from matlab codes.

%% header comments
% description of function
% explanations of args
%... 

function out = myfunc(in)
...
% in-code comment
...
%%

I tried grep "^%" *.m, but it outputs all comment lines.

I want the comments in the head only

%% header comments
% description of function
% explanations of args
%... 

So, it should stop before function out = myfunc(in). Preceding spaces and empty lines are neglected.

How can I make that?


Solution

To extract all comments before the function keyword, you can use sed with two-level filtering:

sed -n '1,/^function/{/^%/p}' file

For all the lines (from the first) until the function, print only those that start with the comment character % (skipping non-comment and empty lines).


In detail. The first part, n,/regex/ is a range address, with the starting address in line 1, and ending address in the first line (after 1) that starts with function string.

If function is not guaranteed to occur, you could use [[:alnum:]] or [[:alpha:]] instead. On GNU sed, additional name for word characters is available, \w.

The second part, {/regex/ p} is executed only for lines previously selected (addressed). And the p (print) action is executed only for the lines that start with %.

Also note the -n flag: it prevents sed from printing each line by default. Line is printed only with an explicit p action.


Example:

$ cat file
%% header comments
% description of function
% explanations of args

% date: ...
% author: ...

function out = myfunc(in)
...
% in-code comment
...
%%

Result:

$ sed -n '1,/^function/{/^%/p}' file
%% header comments
% description of function
% explanations of args
% date: ...
% author: ...


Answered By - randomir
Answer Checked By - Cary Denson (WPSolving Admin)