views:

44

answers:

1

I have a file with function prototypes like this:

int func1(type1 arg, int x);

type2 funct2(int z, char* buffer);

I want to create a script (bash, sed, awk, whatever) that will print

function = func1 // first argument type = type1// second argument type = int function = func1 // first argument type = int// second argument type = char*

In other words, tokenize every line and print the function names and arguments. Additionally I would like to hold these tokens as variables to print them later, eg echo $4.

A: 

here's a start.

#!/bin/bash
#bash 3.2+
while read -r line
do
  line="${line#* }"
  [[ $line =~ "^(.*)\((.*)\)" ]]
  echo  "function: ${BASH_REMATCH[1]}"
  echo  "args: ${BASH_REMATCH[2]}"
  ARGS=${BASH_REMATCH[2]}
  FUNCTION=${BASH_REMATCH[1]}
  # break down the arguments further.
  set -- $ARGS
  echo "first arg type:$1 , second arg type: $2"
done <"file"

output

$ ./shell.sh
function: func1
args: type1 arg, int x
first arg type:type1 , second arg type: arg,
function: funct2
args: int z, char* buffer
first arg type:int , second arg type: z,
ghostdog74
how can I store the backreference in sed? eg echo "abcd" | sed 's/ab\(.*\)/\1/' this prints "cd". How can I store cd in a variable?
cateof
`var=$(echo "abcd" | sed 's/ab(.*)/\1/')`
ghostdog74
almost there. I have the line "int func(struct type1* tp, TYPE1 *name, TYPE2 *name2);". I want to store in a variable the TYPE1 and TYPE2 and print them later
cateof