sed group matching
Solution 1
If you know the number of parameters and they are always 'simple' (no nested parentheses and hence no embedded commas either), then:
echo "func_name(4234,53543,76,1)" |
sed 's/.*(\([^,)]*\),\([^,)]*\),\([^,)]*\),\([^,)]*\))/a1 \1; a2 = \2; a3 = \3; a4 = \4/'
Note that this tolerates spaces after the commas (and before them too - but you wouldn't leave spaces before, would you?).
Or, if the parameters are simple unsigned integers and you know the function name, maybe:
echo "func_name(4234,53543,76,1)" |
sed 's/func_name(\([0-9]*\), *\([0-9]*\), *\([0-9]*\), *\([0-9]*\))/a1 \1; a2 = \2; a3 = \3; a4 = \4/'
To get all the parameters in a single match, you have to do nested grouping:
echo "func_name(4234,53543,76,1)" |
sed 's/func_name(\(\([0-9]*\), *\([0-9]*\), *\([0-9]*\), *\([0-9]*\)\))/args = \1/'
Now \2
.. \5
still refer to the separate arguments.
echo "func_name(4234,53543,76,1)" |
sed 's/func_name(\(\([0-9]*\)\(, *\([0-9]*\)\)\{3\}\))/args = \1/'
This uses the repeat control \{3\}
to find the arguments after the first.
Solution 2
echo 'func_name(4234,43543,76,1)' | cut -d "(" -f2 | sed 's/[,)]/ /g'
Solution 3
This might work for you:
echo 'func_name(4234,43543,76,1)' | sed 's/[^0-9,]//g;y/,/ /'
4234 43543 76 1
Or more verbosely:
echo 'func_name(4234,43543,76,1)' |
sed 'h;s/[^0-9,]//g;y/,/\n/;x;s/(.*/ parameters are:/;G'
func_name parameters are:
4234
43543
76
1
Related videos on Youtube
Jonathan Leffler
Long-time Informix user and developer, experienced in C and Unix (many variants). Email: [email protected] Some of my answers to (or thoughts about and experiments towards answers to) Stack Overflow Questions are available on GitHub in my SOQ repository.
Updated on June 04, 2022Comments
-
Jonathan Leffler almost 2 years
I'm trying to do a bit of group matching using sed.
Basically I have something like this:
func_name(4234,43543,76,1)
And I need to match the parameters of the function:
$ echo 'func_name(4234,43543,76,1)' | sed -n 's/\([0-9][0-9]*\).*/\1 /p' func_name(4234 $ echo 'func_name(4234,43543,76,1)' | sed -n 's/\([[:digit:]]+\).*/\1 /p' <empty> $ echo 'func_name(4234,43543,76,1)'| sed -n 's/.*\([0-9][0-9]*\).*/\1 /p' 1
-
Jonathan Leffler over 12 yearsDo you know how many parameters? Do you need to handle expressions like
34 + 45
? Do you need to handle nested parentheses likefunc_name(abs(b), sqrt(c), hypot(sqrt(d*e + f*g), abs(h-4)), 1)
? -
Admin over 12 years4 parameters, unsigned integers
-
Jonathan Leffler over 12 yearsOK: a fixed known number of simple arguments is relatively straight-forward. Expressions without parentheses are only marginally harder; nested parentheses, possibly with commas in the argument lists, is a whole different ball-game, and not one to attempt with regular expressions.
-
-
Admin over 12 yearsyeah, I know, but I thought I could solve it with only one group and iterate in a loop to get \1 \2 \3 \4.
-
Admin over 12 yearslast one is not correct since you're matching on the comma also
-
Jonathan Leffler over 12 yearsLast two both worked for me...but I was expecting them to include the commas. If you want the arguments in a single capture, you're going to have to include the commas. If you want four separate arguments, you have to request four separate captures.