Reputation: 107
I want to do sub-string match within .sh file and extract list of cmds and append it an existing file.
So far I'm using perl -ne
and it works with last-matched value shown on screen as
echo 'declare -a org_val=($(chkconfig --list autofs) $(grep "^PROMPT="/etc/sysconfig/init)' |
perl -pe 's|.*\$\((.*?)\)\s+|\1|g'
The following is the output of above command:
grep `"^PROMPT=" /etc/sysconfig/init`
I also want it to output
chkconfig --list autofs
What I did was write a small .sh to and save results of sed in array using command substitution below
#!/bin/bash
nl='\n'
declare -a array0
while IFS=$nl read -r line
do
array0+=$line
#echo $line
done < <( perl -pe 's|.*\$\((.*?)\)\s+|\1|g') < /tmp/sunny
echo "${array0[@]}"
The output of above is
declare -a org_val=($(chkconfig --list autofs) $(grep "^PROMPT=" /etc/sysconfig/init)
content of /tmp/sunny
is
declare -a org_val=($(chkconfig --list autofs) $(grep "^PROMPT=" /etc/sysconfig/init) )
Upvotes: 1
Views: 87
Reputation: 204015
With GNU awk for multi-char RS:
$ echo 'declare -a org_val=($(chkconfig --list autofs) $(grep "^PROMPT="/etc/sysconfig/init)' |
awk -v RS='[$][(][^()]+' 'RT{print substr(RT,3)}'
chkconfig --list autofs
grep "^PROMPT="/etc/sysconfig/init
Upvotes: 0
Reputation: 386386
perl -nle'print for /\$\(([^)]*)\)/g'
Of course, that assumes that no )
exists within $(...)
.
Upvotes: 1