I’m trying to scan a file for lines containing a specific string, and print the lines to another file.
However, I need to print out multiple lines until “)” character IF the line containing the string ended in “,” ignoring whitespaces.
Currently I’m using
for func in $fnnames
do
sed/"$func"/p <$file >>$CODEBASEDIR/function_signature -n
done
where $func contains the string I look for, but of course it doesn’t work for the restriction.
Is there a way to do this? Currently using bash, but perl is fine also.
Thanks.
Your question is tricky because your restrictions are not precise. You say – I think – that a block should look like this:
Where
foois the string that starts the block, and closing parenthesis ends it. However, you could also be saying:And you only want to print until the
), which is to sayfoo bar baz), IF the line ends with comma.You could also be saying that only lines that end with a comma should be continued:
Since I can only guess that you mean the first alternative, I give you two options:
The first one will print any lines found between a line starting with
fooand a line ending with). It will ignore the “lines end with comma” restriction. On the positive side, it can be simplified to a one-liner:The second one is just a simplistic if-structure that will consider both restrictions, and warn (print to STDERR) if it finds a line inside a block that does not match both.