- You shouldn't need to read the CSV into variables, you can just loop on the CSV directly:
cat data.csv | while IFS=, read id colour; do # something with $id and $colour
Doing var=$(echo text)
is kind of redundant - you should just use var="text"
directly.
I'm not sure what you mean by the construct [ "grep -E..." ]
, but that is basically testing if the string in the test is not empty, which it isn't because you just put some text in it.
When writing bash scripts it is a good idea to test each expression in turn and see that it behaves as you expect, from the command line (*) - this way a lot of surprises will be eliminated. Writing a complete script and then running it, without a lot of experience on how to write scripts well, is probably a good way to shoot yourself in the foot. I have many years of experience writing bash scripts, and I still try and fail a lot on the command line before putting things down to script. When you do run your script, running it with -x
to enable tracing of the commands will prove very helpful.
I would guess that the main reason you can't get the replacement to work is that the SVG file uses double quotes ("
) for the ID attribute and not single quotes as in your sed
expression.
When constructing sed
expressions in a shell script, its often annoying to deal with all the escaping. If possible I usually like to have the sed
expression not quoted at all, which is one of the reasons why I often use regexp delimiters that aren't slashes (as we will next see). You need to put in the values of the id
and colour
into the sed
expression while also correctly identifying the quoting of the SVG attributes. One thing that might work would be this:
sed -i "s,id=\"$id\",id=\"$id\" style=\"fill:$colour;\"," "$1"
Here we just used double quotes around the expression - which allows us to embed the variables - and just escape the double quotes to match to prevent them from terminating the shell string.
Another option would be to composed the sed
expression from multiple strings that have different quoting rules:
sed -i 's,id="'"$id"'",id="'"$id"'" style="fill:'"$colour"';",' "$1"
Here we put all the static text in single quotes, and then just terminate the single quotes and open a double quoted string to have the variables parsed. This works because there are no spaces between the single quoted strings and the double quoted strings, and as long as there are no spaces, Bash passes all that as a single argument to sed
. But Obviously that's a bit confusing, so I prefer the first style.
Another thing that we can do, is to use sed
capture groups to not type id="$id"
more than once:
sed -i "s,\(id=\"$id\"\),\1 style=\"fill:$colour;\"," "$1"
The \(
and \)
capture the thing that was matched, and \1
drops it back into the substituted string.
*) If you have done scripting in MS-Windows you are probably aware of the sad sad situation there where the command line (whether CMD or powershell) behaves differently then running the same commands in scripts, in often non-obvious ways. This is not the situation in Unix scripting shells such as Bash.