SED expression for turning Yeseva+One into --font-yeseva-one: “Yeseva One”;

一个人想着一个人 提交于 2019-12-13 08:09:33

问题


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


回答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";



回答2:


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";



回答3:


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)



来源:https://stackoverflow.com/questions/43085748/sed-expression-for-turning-yesevaone-into-font-yeseva-one-yeseva-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!