Issue
my goal is to have a one liner to generate a list of variables composed from the name of the variable and its value. The value must be kept "as-is" while the name of the variable must be CamelCase, when removing leading slash "/" or dot "." or dash "-"
Sample of input:
auto-scaling/v2/HorizontalPodAutoscaler
v1/Service
apiextensions.k8s.io/v1/CustomResourceDefinition
Expected output:
AutoScalingV2HorizontalPodAutoscaler.$ : "auto-scaling/v2/HorizontalPodAutoscaler"
V1Service.$ : "v1/Service"
ApiextensionsK8sIoV1CustomResourceDefinition.$ : "apiextensions.k8s.io/v1/CustomResourceDefinition"
I found some clue in stackoverflow to transform a full string in CamelCase using awk or sed but those are for the full line while I would only what the part before the .$" (or the ":" as I could add after the ".$ " part)
echo "apiextensions.k8s.io/v1/CustomResourceDefinition" | sed -r 's/(^|[-_/\.]+)([0-9a-z])/\U\2/g'
ApiextensionsK8sIoV1/CustomResourceDefinition
echo "apiextensions.k8s.io/v1/CustomResourceDefinition" | awk 'BEGIN{FS="";RS="[-./]";ORS=""} {$0=toupper(substr($0,1,1)) substr($0,2)} 1'
ApiextensionsK8sIoV1CustomResourceDefinition
Solution
You can use the below awk to achieve your purpose:
awk 'BEGIN{FS="";RS="[-./]";ORS=""} {$0=toupper(substr($0,1,1)) substr($0,2)} 1'
Explanation of the above command:
BEGIN{FS="";RS="[-./]";ORS=""}
- Set the separators.FS=""
- Field Separator (FS) is set to make each character behave like a separate field.RS="[-./]"
- Record Separator (RS) is set to a regex which matches[-./]
and separates according to it. You can add or remove them based on your specific purpose.ORS=""
- Output Record Separator (ORS) is set to avoid printing new lines.
{toupper(substr($0,1,1))
- It will take the first character of record and converts it to uppercase and prepend it to the output.substr($0,2)
- Appends the rest of input to the output record.1
- shorthand to{print}
current record.
Answered By - mandy8055 Answer Checked By - Marilyn (WPSolving Volunteer)