After a couple of hours trying to get my sed
query to work, I'm about to give up!
I have the following string, extracted from source code - the aim is to generate prototypes for a lot of old and undocumented code. For example:
function foo(bar=1);
I want to end up with something like :
function foo(
bar=1)
I want to match all lines that start with function
followed by a random alphanumerical (and - and _) word, and add line breaks and tabs with sed
.
My problem is that sed is greedy by default, and I cannot get sed
to add a line break after the first (
due to regex greedyness.
So something like this hardcoded works:
echo 'function foo(bar=true)' | sed 's:\(function foo(\)\(.*\):\1\n\t\2:g'
which gives me the expected output:
function foo(
bar=true)
I can modify this to the following, adding the newline after the (
character:
echo 'function foo(bar=true)' | sed 's:\(function.*(\)\(.*\):\1\n\t\2:g'
Which gives the same expected result as before - until I get to a function in the code that contains an array as a default value for an argument - which is where the greedy regex trips me up:
echo 'function foo(bar=array())' | sed 's:\(function.*(\)\(.*\):\1\n\t\2:g'
Which actually gives:
function foo(bar=array(
))
The greedyness causes the newline and tab to be added after the last (
and not the first. Unfortunately sed
does not support non greedy regex which would solve everything immediately...
So I tried trying to make a regex that would do something like below, but I'm not getting anywhere:
's:\(function [\w+]\)\(.*\):\1\n\2:g'
trying to extract an alpha numericals which should match everything to the first (- Doing the same trying using an
:alnum:
class to match the word - Doing the same replacing with the more
sed
friendly[A-Za-z0-9_-]
but having difficulties understanding how to get this to match more than 1 character in the pattern, so it takes the word up to the first(
- and then get the rest in the second return.
It seems that these character classes are just being ignored in the query, and I'm out of ideas.
As I cannot make sed
non-greedy, how can I match a string which is of the format
KnownKeyword SomethingRandomAlphaNumerical-_(SomethingElse())
into a string that after line breaking after the first (, will look like:
KnownKeyword SomethingRandomAlphaNumerical-_(
SomethingElse())
Where am I going wrong? What pattern would accomplish this?