Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CamelCase only for some part of the line using shell tool

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

  • {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.

CODE DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading