-1

I have a series of font values like this (Command separated one line):

Yeseva+One, Yrsa, ...

I'm looking for a SED (Or other bash tool expression) for turn each value into line statements like this:

--font-yeseva-one: "Yeseva One";
--font-yrsa: "Yrsa"; 

Thoughts?

Update

Just wanted to say thanks for all the great help with this and if anyone needs google fonts as CSS variables / properties they are all available here (MIT License): https://github.com/superfly-css/superfly-css-variables-fonts/blob/master/src/main/css/index.css

I'll also be providing utilities for using google fonts here: https://github.com/superfly-css/superfly-css-utilities-fonts

Ole
  • 41,793
  • 59
  • 191
  • 359

3 Answers3

1
#!/bin/sh

variable="Abc, Def+Hola, Ghij"

IFS="[, ]"
for i in $variable
do
        a=`echo "$i" | sed 's/\+/ /g'`
        i=`echo "$i" | tr '[:upper:]' '[:lower:]' | sed 's/\+/\-/g'`
        echo "--font-$i: \"$a\";"
done

Please check, I have checked it on my machine and it works fine!

Output:

--font-abc: "Abc";
--font-def-hola: "Def Hola";
--font-ghij: "Ghij";
Ashish K
  • 905
  • 10
  • 27
  • Thanks - Almost there ... I love the simplicity of it BTW - very easy to follow - however some of the output looks like this: --font--abhaya+libre: Abhaya+Libre ... and it needs to look like this --font--abhaya-libre: "Abhaya Libre" – Ole Mar 29 '17 at 07:11
  • Hey @Ole, I think the same code works for me when I add "Abhaya+Libre" to the list of input parameters. Please tell me if I am missing something. – Ashish K Mar 29 '17 at 07:19
  • Works perfectly now - Thanks!! – Ole Mar 29 '17 at 07:32
0

awk approach:

s="Yeseva+One, Yrsa, Courier+New, Alegreya+Sans+SC"
awk -F", " '{for(i=1;i<=NF;i++){k=gensub("+","-","g",$i);
     v=gensub("+"," ","g",$i);printf "--font-%s: \"%s\";\n",tolower(k),v;}}' <<< $s

The output:

--font-yeseva-one: "Yeseva One";
--font-yrsa: "Yrsa";
--font-courier-new: "Courier New";
--font-alegreya-sans-sc: "Alegreya Sans SC";

-F", " - field separator

(i=1;i<=NF;i++) - iterating through all the fields

k=gensub("+","-","g",$i); - replacing all + with - (for font key attribute)

v=gensub("+"," ","g",$i); - replacing all + with (for font value)

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Hi - I put the above in a script and I got the following: ./script2.sh: 7: ./script2.sh: Syntax error: redirection unexpected ... is there something I'm doing wrong? – Ole Mar 29 '17 at 07:15
  • @Ole, test it on command line. Another way to pass the input is `echo "Yeseva+One, Yrsa, Courier+New" | awk -F", " '{for(i=1;i<=NF;i++){k=gensub("+","-",1,$i); v=gensub("+"," ",1,$i);printf "--font-%s: \"%s\";\n",tolower(k),v;}}'` – RomanPerekhrest Mar 29 '17 at 07:18
0

With Pure Bash :

IFS="[, ]" read -r -a list <<<"Yeseva+One, Yrsa"  #Convert values to array
for k in "${list[@]}";do 
  k2="${k,,}" #The bash way to convert everything from upper to lower
  printf -- '--font-%s:\"%s\";\n' "${k2//+/-}" "${k//+/ }"  #bash way to replace strings
done
#Output
--font-yeseva-one:"Yeseva One";
--font-yrsa:"Yrsa";
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27