Search code examples
node.jstypescriptshellgoogle-cloud-functionsmultiline

How to grep cloud functions in a multiline format separated by comma?


I have been trying to fetch the cloud functions using grep/awk/sed however the current command is not fetching the cloud functions listed in a multiline format.

This is the current command that is being passed as a variable during runtime:

deploy.sh

functions_list=$(grep 'export' src/index.ts | awk -F '[}{]' '{print $2}' | sed 's/^ *//; s/ *\$//; /^\$/d' | grep -v somefunction || echo '')

index.ts

export { functionA } from './functions';
export { functionB } from './functions';
export {
    functionC,
    functionD,
    functionE
} from './functions';
export { functionF } from './functions';
export { functionG } from './functions';

Then the output will be like this:

functionA
functionB

functionF
functionG

Anyone who has any idea on how to have the output to be like this instead?

functionA
functionB
functionC
functionD
functionE
functionF
functionG

Solution

  • See final command:

    functions_list=$(awk 1 ORS=' ' src/index.ts | sed -e 's/    / /g ; s/;/\n/g' | awk -F '[}{]' '{print $2}' | sed 's/^ *//; s/ *\$//; /^\$/d; s/,  /\n/g' | grep -v somefunction || echo '')
    

    awk reads/scans input line by line; hence skips the functionC, functionD and functionE as they do not fall under the provided pattern:

    '[}{]' '{print $2}'
    

    I've added awk/sed commands to initially replace new line with whitespaces.

    awk 1 ORS=' ' src/index.ts | sed -e 's/    / /g ; s/;/\n/g'
    

    Then, finally, to replace commas with newline.

    s/,  /\n/g