Question: I want to write a bash script that takes a user input (which will be a filename) and replaces a path to a file inside a css file with that filename. For simplicity, the two files will be in the same folder and in the css code only the filename at the end of the path should be changed.

I thought of using regex to match any line of code that has a specific pattern and then change the end of it. I know about sed, but since the filename always changes I'm not sure how to solve this problem other than regex. I also thought of adding a variable in the css file that holds the filename as a value and then adding that variable at the end of the path, but I'm not sure then how to access that variable from a bash script.

Any recommendations on how to tackle this problem?

Thanks!

**Edit Adding more Information:** 
Here is the line in the css file I want to edit. The part to be changed is the fileName.png at the end. Since it will change I thought of using a regex to "find" the correct spot in the css file.

```
background: #2c001e url(file:////usr/share/backgrounds/fileName.png/);

```

A regex matching only this line in this specific file is the following. It could probably be simplified, but I don't see a reason why since it should work too:)

```
(background)\:\s\#.{6}\s(url)\((file)\:\/{4}(usr)\/(share)\/backgrounds\/.+\.(png)\/\)\;

```

Answer: 
This subtracts the first r level from the first g level, second r level from second g level, etc. Same for type1 and type2. This has no checks at all. It doesn't check whether there is a matching r for each g, whether they are in the expected order, whether they are in the same date-group, etc. It assumes the data is already perfectly formatted as expected, so be careful using this on real data.

```
d %>% 
  mutate(color_gap = replace(rep(NA, n()), colour == 'g', 
                             level[colour == 'g'] - level[colour == 'r']),
         type_gap = replace(rep(NA, n()), type == 'type1', 
                             level[type == 'type1'] - level[type == 'type2']))
# # A tibble: 10 x 6
#    date  colour type  level color_gap type_gap
#    <chr> <chr>  <chr> <dbl>     <dbl>    <dbl>
#  1 2018  none   type1    78        NA       11
#  2 2018  g      none     99        54       NA
#  3 2018  r      none     45        NA       NA
#  4 2019  none   type2    67        NA       NA
#  5 2019  g      none     87         9       NA
#  6 2019  r      none     78        NA       NA
#  7 2020  none   none     89        NA       NA
#  8 2020  none   none     87        NA       NA
#  9 2020  none   none     67        NA       NA
# 10 2020  none   none     76        NA       NA

```
you could do this with `group_by` and mutate.

I assumed that there is only 1 row per `date` that would satisfy each condition.

```
d %>% 
  mutate(color_gap = case_when(colour == "g" ~ level)) %>%
  mutate(type_gap = case_when(type== "type1" ~ level)) %>%
  group_by(date) %>%
  mutate(diff = max(color_gap,na.rm=T)-max(type_gap, na.rm=T))

```