3

I want to replace the input,

find_string: @include circle-progress(38px, 30px, #4eb630)

and output,

Output_string: @include circle-progress(38px, 30px)

using ${find_string//pattern/replacement_string} where pattern is , , #[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]?([A-Fa-f0-9]?([A-Fa-f0-9]?([A-Fa-f0-9])))' that I supply.

In the code below, simply the line matching pattern is printed i.e find_string, when I read lines of code from a file, whereas I want the output_string to be printed.

pattern="@include circle-progress\(([0-9]{1,3}px, ){2}#[A-Fa-f0-9]
{3,6}\)" /*regex the matches find_string*/

replace_glob=', #[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]?([A-Fa-f0-9]?([A-
Fa-f0-9]?([A-Fa-f0-9])))' /*glob pattern in the string to be replaced*/

while IFS='' read -r line || [[ -n "$line" ]]; do
   if [[ $line =~ $pattern ]] 
   then
      echo "${line//$replace_glob/}" 
   fi
done < "$1"
5
  • So basically your requirement is remove the part #4eb630 from the input string right? Commented Dec 22, 2017 at 10:04
  • @Inian Yes, That is what I want, using Parameter Expansion only Commented Dec 22, 2017 at 10:13
  • You can't use parameter expansion of type ${source/orig/repl/}, the variables orig and repl cannot be vaiables, they can just be strings. You need to use sed for replacement Commented Dec 22, 2017 at 10:34
  • @Inian The same concept works if I execute commands echo "${source//$replace_glob/}" with just a single source in interactive mode. It replaces @include circle-progress(38px, 30px, #4eb630) with @include circle-progress(38px, 30px). Commented Dec 22, 2017 at 10:40
  • Use awk or sed; this is the type of task they were created to handle. Commented Dec 22, 2017 at 15:03

2 Answers 2

3

The pattern in parameter expansion is not a regular expression but follows the same rules as glob pattern matching:

  • * : matches any character sequence
  • ? : matches any character
  • [..] : any character in set
  • [^..] or [!..] : any character not in set

with shell option : shopt -s extglob, some more features but less than regular expressions

  • @(..|..) : match any once
  • ?(..|..) : match any 0 or 1 times
  • *(..|..) : match any 0 or more times
  • !(..) : matches all except

However bash supports some basic regex, following should work:

string='@include circle-progress(38px, 30px, #4eb630)'
pattern='@include circle-progress\([ ]*[0-9]{1,3}px,[ ]*[0-9]{1,3}px(,[ ]*#[A-Fa-f0-9]{3,6}[ ]*)\)'
[[ $string =~ $pattern ]] && echo "${string//"${BASH_REMATCH[1]}"}"
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply use sed

echo $line | sed 's/\(@include circle-progress([0-9]\{1,3\}px, [0-9]\{1,3\}px\), #[a-fA-F0-9]\{3,6\})/\1)/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.