Issue
I have a document file.yaml that have something like placeholders to replace:
class: ##TOPIC##-area
name: myClass
type: Class
secretKey: private-##SECRET_KEY##
so far I've used grep to get the values of placeholders
grep -P '(?<=##).*(?=##)' file.yaml
then, I had those values:
TOPIC
SECRET_KEY
now, we have to introduce new properties that can have more than one placeholder per line
class: ##TOPIC##-area
name: myClass
type: Class
secretKey: private-##SECRET_KEY##-encoded-##SUFFIX_CODE##
hence, grep no longer worked because the output became:
TOPIC
SECRET_KEY##-encoded-##SUFFIX_CODE
but, I want to have
TOPIC
SECRET_KEY
SUFFIX_CODE
I accept all kinds of suggestions and ideas to solve that. thanks
edit: the idea is to just get those placeholders, not replace them. sorry for the misunderstanding.
Solution
1st solution: With using GNU awk
, please try following awk
code. Simple explanation would be, setting RS(record separator) as ##
till next occurrence of #
followed by 2 occurrences of ##
. Then simply printing matched lines only by removing the not needed ##
in outcome as per shown output samples.
awk -v RS='##[^#]*##' 'RT{print substr(RT,3,length(RT)-4)}' Input_file
2nd solution: With any awk
, please try following program. Simple explanation would be, using match
function of awk
to match regex /##[^#]*##/
(explained in above first solution already); in a while loop to print all the matches found in each line.
awk '{while(match($0,/##[^#]*##/)){print substr($0,RSTART+2,RLENGTH-4);$0=substr($0,RSTART+RLENGTH)}}' Input_file
Answered By - RavinderSingh13