Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Replace after searched string with a variable (containing slashes) in sed/awk

$
0
0

Tools

  • rxvt-unicode (urxvt) v9.31
  • GNU bash, version 5.2.26(1)-release (x86_64-pc-linux-gnu)
  • GNU Awk 5.3.0
  • sed (GNU sed) 4.9
  • tr (GNU coreutils) 9.4

Attempted behaviour

  • Grab the first 9 colours from ~/.colour_list which has the colours printed with a newline between each one
  • Remove the leading # from each colour
  • Place a / every 2nd character; FF/FF/FF/
  • Append each colour with a transparency value; FF/FF/FF/2F
  • Prepend each colour with the string rgba:; rgba: FF/FF/FF/2F
  • Place a , every 16 characters (except for the last character) to create a comma separated list; rgba: FF/FF/FF/2F, rgba: 2F/AA/66/2F ...
  • Place this entire comma separated list into a variable bc
  • Place the variable bc after the exact string match bar_color = (including spaces) in the file test.conf

Issue

I have two issues.

  1. The $bc assignment (see sed 's/.\{2\}/&\//g;s/.$//') removes all commas, where I would only like to remove the final comma (i.e. the final character) of the entire string.

  2. Upon finding the string, colour = , I want to place the value of $bc after it but I instead get an error (see below)

The script is as follows;

value=9transparency=FFbc=$(head -n${value} ~/.colour_list |     tr -d '#' |     sed 's/.\{2\}/&\//g;s/.$//' |     awk '{print "rgba:" $0 transparency}' transparency=$transparency |     sed 's/.\{16\}/&,/g')sed -ri "s|^(bar_colour             \s*=\s*).*|\1$bc|" ~/test.confecho $bc

Output and expected output

Input is the script above called via (bash) terminal.

Output:

sed: -e expression #1, char 52: unterminated `s' commandrgba:21/1c/3b/FF rgba:CB/68/75/FF rgba:6E/5A/86/FF rgba:74/5A/84/FF rgba:89/5B/83/FF rgba:8F/66/8B/FF rgba:AC/68/84/FF rgba:d8/ab/b7/FF rgba:97/77/80/FF

~/test.confnot changed;

...bar_colour              = test...

Expected output:

rgba:21/1c/3b/FF, rgba:CB/68/75/FF, rgba:6E/5A/86/FF, rgba:74/5A/84/FF, rgba:89/5B/83/FF, rgba:8F/66/8B/FF, rgba:AC/68/84/FF, rgba:d8/ab/b7/FF, rgba:97/77/80/FF

~/test.conf changed;

...bar_colour              = rgba:21/1c/3b/FF, rgba:CB/68/75/FF, rgba:6E/5A/86/FF, rgba:74/5A/84/FF, rgba:89/5B/83/FF, rgba:8F/66/8B/FF, rgba:AC/68/84/FF, rgba:d8/ab/b7/FF, rgba:97/77/80/FF...

Methods tried

I have tried previous answers (1, 2, 3, 4) but none of them seem to work and invariably return as above.

I am guessing for the awk error at least, this is because the variable I am trying to replace with contains slashes, but I am not sure why sed is throwing an error. I require the slashes as the expected format is as per XQueryColor specifications.

Thank you very much.

EDIT:

Changed script to the following, as per the comments;

value=9transparency=FFbc=$(head -n${value} ~/.colour_list |     tr -d '#' |     sed 's@.\{2\}@&\/@g;s@.$@@' |     awk '{print "rgba:" $0 transparency}' transparency=$transparency |     sed 's@.\{16\}@&,@g')sed -ri "s|^(bar_colour             \s*=\s*).*|\1$bc|" ~/test.confecho $bc

This also provided the same errors as per the Output section, unfortunately.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>