-1
#!/bin/bash

current_day = "6"
sed_output=$(sed -E "s/\b${current_day}\b/$(tput setaf 1)&$(tput sgr0)/g" "calendar.txt")

if echo "$sed_output" | grep -q "$current_day"; then
   echo "Value '$current_day' found in the modified output."
   sed -i "s/$(tput setaf 1)$(printf '%s' $current_day)$(tput sgr0)/$current_day/g/" calendar.txt
   #echo "Modifier removed."
else
    echo "Value '$current_day' not found in the modified output."
fi

calender.txt

I keep getting an error message: "sed: -e expression #1, char 19: unterminated `s' command" The code should be removing the modifier and make the value unmodified.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bagas Bima
  • 19
  • 1
  • 2
    Use `set -x` to enable logging and see what the actual sed command generated by your code is; one of the expansions is inserting content you don't expect. – Charles Duffy Jun 06 '23 at 16:45
  • copy/paste your code into https://shellcheck.net including `#!/bin/bash` as your fist line, and fix any errors flagged. If that doesn't solve your problem, please update your Q with corrected code. Good luck. – shellter Jun 06 '23 at 16:45
  • 2
    Also, note that backslash sequences like `\b` aren't guaranteed to work in `sed`. Read the POSIX BRE and ERE specifications for details of exactly what _is_ guaranteed to work, and if you want your code to be reliable/portable, use only those features and nothing else. – Charles Duffy Jun 06 '23 at 16:46
  • Not a great question, but better than average for a first time post. I've upvoted already, but hope you will read the section "How to turn a bad script into a good question" in https://stackoverflow.com/tags/bash/info and improve your question. Sample input and expected output and **shellcheck**ed code are key to a good question. Good luck. – shellter Jun 06 '23 at 16:49
  • I can't reproduce the error. What version of `sed` are you using? – Barmar Jun 06 '23 at 17:30
  • 2
    This error generally indicates that the output of one of the `tput` commands contains a newline. – Barmar Jun 06 '23 at 17:31
  • 3
    The output from `tput` presumably contains `[` characters, which have special meaning in regular expressions, and need to be escaped if you want to match them as literal characters. See ["Is it possible to escape regex metacharacters reliably with sed"](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) for recipies for adding the necessary escape(s). Also, `$(printf '%s' $current_day)` is unnecessarily complicated (unless the variable has newlines at the end?); just use `$current_day` directly. – Gordon Davisson Jun 06 '23 at 17:36
  • don't post bash questions unless you've run set -x and have the output – xpusostomos Jun 06 '23 at 18:52
  • Your script is missing the line `cal > calendar.txt` and `current_day = "6"` should be `current_day="6"`. – Walter A Jun 06 '23 at 19:14

1 Answers1

0

The tput commands insert special characters in your string. You can see the characters with od:

tput setaf 1 | od -c
0000000 033   [   3   1   m
0000005
tput sgr0 | od -c
0000000 033   (   B 033   [   m
0000006

You can use a different approach: remove everything that is not a space or digit.
You only want to perform this action on the lines with days of the month, not on the header lines.
As a result, for all lines with an alpha character in it, don't (that is !) use the remove command that will replace everything except digits and spaces [^0-9 ] with nothing.

sed -r '/[[:alpha:]]/!s/[^0-9 ]//g' calendar.txt 
Walter A
  • 19,067
  • 2
  • 23
  • 43